Reputation: 695
I have this dataset at the moment that I am using for a quiz application (how bad is it?):
var questions =
[
'Question 1 text',
'Question 2 text'
]
var answers =
// Question 0
[
['Choice 0', // Right answer
'Choice 1',
'Choice 2']
]
// Question 1
[
['Choice 0',
'Choice 1', // Right answer
'Choice 2']
]
var correctChoice =
[
'0', // Question 0, choice 0
'1' // Question 1, choice 1
]
So I am relying on the "invisible index" to link them all together, which is kinda hard to maintain. Is there a better way to go about this? Are objects better? Recommendations, or best practices in general?
I have been thinking about JSON - would that be a good alternative?
Upvotes: 3
Views: 2694
Reputation: 54810
Something like this should do:
var quiz = [
{question: 'Question 1 text', answers: ['answer 1', 'answer 2'], correct: 0},
{question: 'Question 2 text', answers: ['answer 1', 'answer 2'], correct: 1}
];
Upvotes: 9
Reputation: 11352
var quiz = {
questions: [
'Question 1 text',
'Question 2 text'
],
answers: [
[//Question 0
'Choice 0', // Right answer
'Choice 1',
'Choice 2'
],
[//Question 1
'Choice 0',
'Choice 1', // Right answer
'Choice 2'
]
],
correctChoice: [
'0', // Question 0, choice 0
'1' // Question 1, choice 1
]
}
Upvotes: 0