choices is a nested array in json object i am placing this data for a multiple choice quiz sequence. When i press the trigger for the function the console says 'Uncaught TypeError: Cannot read property 'choices' of undefined' i also have a variable 'c' which changes when the user selects another exam so the the statement exam=exam+c would mat the placement function work.
var exam0 = [
{
"question": "which is a negative number?",
"choices": [
"2",
"-2",``
"6",
"8",
],
"correctAnswer": "B",
"hint": "The one with the ' - ' negative sign"
},
......other questions****
];
The function is
function placement(x) /*x is variable used to change the question from the json object*/
{
choiceOne=$('<p>').text('A.'+exam[x].choices[0]); /*where the console points to problem*/
choiceTwo=$('<p>').text('B.'+exam[x].choices[1]);
choiceThree=$('<p>').text('C.'+exam[x].choices[2]);
choiceFour=$('<p>').text('D.'+exam[x].choices[3]);
currentQuestion=$('<p>').text(exam[x].question);
$("#honeyPot").empty().append(currentQuestion);
$("#honeyPot p").prepend(count+".");
$('#options').find('p').remove().hide();
$('#optionOne').fadeIn(250).append(choiceOne);
$('#optionTwo').fadeIn(250).append(choiceTwo);
$('#optionThree').fadeIn(250).append(choiceThree);
$('#optionFour').fadeIn(250).append(choiceFour);
}
I think you should use exam0 in your function too instead of exam since exam0 is the json object.
function placement(x) /*x is variable used to change the question from the json object*/
{
choiceOne=$('<p>').text('A.'+exam0[x].choices[0]); /*where the console points to problem*/
choiceTwo=$('<p>').text('B.'+exam0[x].choices[1]);
choiceThree=$('<p>').text('C.'+exam0[x].choices[2]);
choiceFour=$('<p>').text('D.'+exam0[x].choices[3]);
currentQuestion=$('<p>').text(exam0[x].question);
$("#honeyPot").empty().append(currentQuestion);
$("#honeyPot p").prepend(count+".");
$('#options').find('p').remove().hide();
$('#optionOne').fadeIn(250).append(choiceOne);
$('#optionTwo').fadeIn(250).append(choiceTwo);
$('#optionThree').fadeIn(250).append(choiceThree);
$('#optionFour').fadeIn(250).append(choiceFour);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.