0

I am a complete beginner and I am working on creating a multiple choice quiz using array. The questions have the same format " Which word in the following has a similar meaning to '_________' ". So I create an array(?) that store all the words I want to put in '________' and another array that contain answers correspond to it.

var ques=['apple', 'pencil' ,'juice'];
var ans= ['orange', 'pen','water'];

However, as I want to create a few hundreds of questions, I think it is quite troublesome to do it in the following way.

var querep=[ ['apple','orange'], ['pencil','pen'],['juice','water'] ];

so i try doing this instead:

var querep=[ [ques, ans] ];

and yes, i know it makes no sense to u all, but i just want to merge the two array lists and allow it to do the same function as
var querep

example questions: which word in the following has similar meaning to apple?
A.pen
B.writer
C.orange
D.vegetable

2
  • so the point is you want join array like this [ques, answer] from original array? Commented Nov 21, 2019 at 4:39
  • @illuminarch yes Commented Nov 21, 2019 at 4:41

3 Answers 3

1

This answer may not be fulfilled with your approach. But you can have it as reference for data structure design. Since years ago I did something similar to this and finally found myself immerse in refactor phase and re-do a lot of parts.

I can tell you may have a big list of question survey, when it comes to long list. You should think of it as a big object. Not a usual Array like the approach you're implementing, because it's faster for any things related to find/ traverse purpose.

So, basically the data might look like this:

const questions = {
  "apple": "orange",
  "water": "juice",
  "chicken": "duck",
  ...
}

You still can iterate through an object with {key,value} pair and your problem is solved.

In real scenario, I think the data structure might be a more complicated, usually each will have its own _id, so the object might look like this, but the approach of iterating doesn't change.

const questions = {
  "12312311414": {
    id: "12312311414",
    quesion: "apple",
    accept: "orange",
  },
  "12312311415": {
    id: "12312311415",
    quesion: "water",
    accept: "juice",
  },
  ...
}

So, before deciding merging two arrays, I hope you can change your mind.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use a merged array as mentioned in question, you could use map function on array to create a new array. Here new querep array should be in the format as you want:

var ques=['apple', 'pencil' ,'juice'];
var ans= ['orange', 'pen','water'];
var querep = ques.map(function(val, index){
  return [val, ans[index]]
});

2 Comments

please carefully checking index in this case, if either ques or ans is mistached with each other, your survey data will be corrupted
the data are in correct order, thanks for pointing that out as well. i also want to ask how to display one question at a time. the solution i found on internet did not seem to solve the problem for me
0
plan like this.

    // At first write all your questions , options and answers here.
    var questions = [
        {"question" : "what is the capital of india?" , "options" : ["hyderabad","delhi","chennai","bengalore"], "answer" : "hyderabad"},
        {"question" : "what is even number?" , "options" : [1,3,5,8], "answer" :8}
    ];


    // you will get question along with question number here
    var questions_list = {}; var i = 1;
    $.each(questions ,function(k,v){
        questions_list[i] = v;
    i++;
    });

  console.log(questions_list);

1 Comment

thanks for you answer. i have thought of using this plan but as i mentioned, there would be hundreds of questions (all in the same format) presented on the webpage. it would be ineffective for me to construct every single questions. thanks for your kind suggestion though :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.