0

I have a question with multiple answers. I want an array that has two spots, the first can only hold one value (one question), the second can hold an array (multiple answers).

Question: 'favorite colors' Answers: 'blue' 'black' 'red'

var answer = [];
var question [[],answer];

I'm unable to get this to work properly. :(

0

4 Answers 4

3
 var list = [ 'favorite colors', ['red','black'] ];

But I would do something like this instead:

 var question = { question: 'Favorite color',
   answers: ['red','black']
 }

 var list = [ question, ... ];
Sign up to request clarification or add additional context in comments.

1 Comment

this is very interesting, do i need to var list{question:'favorite color',answers:['red','black']}
2

Is it what you wanted?

var question = 'What is your favourite color?';
var answers = ['blue', 'yellow', 'black', 'pink', 'omg, ponies!'];

var your_array = [question, answers];

or without immediate variables:

var your_array = [
    'What is your favourite color?', // question here
    ['blue', 'yellow', 'black', 'pink', 'omg, ponies!'] // answers array here
];

To get the question you do:

var your_question = your_array[0];

To get the list (array) of answers you do:

var your_answers = your_array[1];

4 Comments

how would i pick the second choice(yellow) in the array? just your_array[1][1]?
@user1082764: To get the second answer you will need to do it like that: your_array[1][1]. Its because the arrays are indexed beginning with 0 (zero), and this array is multidimensional. The first dimension choses answers (your_array[1]) that lie on the second position (thus [1]), and the second dimension choses second position within what you get so far (so the second position within the answers).
I am struggling a to understand multidimentional arrays, you've been great help!
@user1082764: Good to know. If it helps, you can treat every part with brackets separately (because it is how it works). your_array[1][1] really means: 1) your_array means "get your_array variable", 2) [1] means "get second element of what you got so far, from point 1)", 3) the second [1] means "get the second element of what you got so far, from point 2)".
1

Try this

var question =  [[], answer];

Comments

0

Doesn't this work?

var question = [[],answer];

Comments

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.