3

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?

2 Answers 2

9

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}
];
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Not really; that's pretty much the same.

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.