I have some quiz questions stored in a JSON that I am calling using AJAX and then stroing as the variable allQuestns:
// Variable stores all quiz questions
var allQuestns;
// AJAX request to retrieve quiz questions from external JSON
$.ajax({
async: false,
type: 'GET',
url: 'https://api.myjson.com/bins/dqt3f',
data: { get_param: 'value' },
dataType: 'json',
success: function (data) {
allQuestns = data;
},
error: function () {
alert("Unable to locate the quiz questions. Try again soon and contact admin - quiz@palmerhinchliffe.net")
}
});
The questions for each quiz are stored in the JSON as an object that refers to that quiz (qz1, qz2, qz3), which each hold an array of questions, with each question array containing a question string, an array of choices, and the correct choice.
I'd like to create a variable that I can use in place of directly referring to the quiz object from the JSON. So instead of:
alert(allQuestns.qz2[2].question) //This retrieves the question text
//from the 3rd question of the 2nd
//quiz
i want to refer to the quiz number using a variable that changes, like so:
var quizCounter = "qz2";
alert(allQuestns.quizCounter[2].question);
Referring to the object using the quizCounter variable doesn't seem to work. Is there a way to do this, or do I have to actually name the object itself rather than create a reference to it that can be changed.
I want to be able to display the right question for each quiz by using a quizCounter, rather than having seperate html divs and JS functions for each quiz