Best data structure for multiple choice quiz in JavaScript

arrays, data-structures, javascript, multidimensional-array, object

Solution

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}
];

Problem

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?

Original source