Reputation: 165
I have taken the following object as an example:
var questions = {
"1": {
"question": "What animal goes 'woof'",
"a": "Duck",
"b": "Cat",
"c": "Dog",
"answer": "Dog"
},
"2": {
"question": "What color is a giraffe",
"a": "Yellow",
"b": "Orane",
"c": "Blue",
"answer": "Yellow"
},
"3": {
"question": "What is 2 + 2",
"a": "6",
"b": "4",
"c": "9",
"answer": "4"
},
"4": {
"question": "What is 4 + 8",
"a": "12",
"b": "17",
"c": "100",
"answer": "12"
},
"5": {
"question": "What is 4 - 1",
"a": "5",
"b": "91",
"c": "3",
"answer": "3"
}
};
I would like to shuffle these questions in a particular way.
I would like to maintain the property name's and order (1,2,3 .etc.), but shuffle the values.
So the first property of 'questions' would still be "1" but the object containing the "question", "a", "b", "c", "answer" would be shuffled.
Hopefully that make's sense, just ask if I've been unclear. Thanks in advance guys!
Upvotes: 1
Views: 1614
Reputation: 328770
Here is a better data structure:
var questions = [
{ "question": "What animal goes 'woof'", "answers": ["Dog","Cat","Duck"] },
]
i.e. the first answer is always correct.
In your code, you can copy the first answer into a variable (so you know the correct answer) and then simply shuffle the answers
array.
Upvotes: 0
Reputation: 38820
Firstly, I don't see the point in having the "1", "2", "3" properties at all, your array just needs to be an array of the question objects. The one at the first index (0) is "1", the second index is "2". This can be inferred by position.
var question = [{question 1 data}, {question 2 data}];
Once you've done that and simplified it, you can use a shuffle function like the one described here.
Upvotes: 3