I want to have an Object with a two dimensional array in it. it will hold questions with associated choices and the correct answer. The first time thru it works correctly but the second time with i= 1 k= 0 I get an exception. Here is the relevant code with the exception thrown after the first entry in the object:
var testquestions = [{
question:[],
choices:[[]],
answer:[],
}
];
var i = 0;
$( "#opener" ).click(function() {
$( "#dialog" ).dialog( "open" );
$( "#dialog" ).dialog({
minWidth: 700,
minHeight: 650,
buttons: [
{
text: "Add Question",
click: function() {
i = testquestions[0].question.length;
testquestions[0].question[i] = $( "#question" ).val();
for(var k = 0; k < 4; k++){
if ($("#" + k).val() != "") {
alert("i and k values are " + i + "nd " + k);
testquestions[0].choices[i][k] = $( "#" + k ).val();
}
} // for k
testquestions[0].answer[i] = $("#correctAnswer").val();
$("#test")[0].reset();
alert(testquestions[0].question[i]);
}
}
exception
TypeError: testquestions[0].choices[i] is undefined
testquestions[0].choices[i][k] = $( "#" + k ).val();
Can someone tell me if I am declaring and invoking the 2-d array correctly? The exception gets thrown when i = 1. Thanks Update: Answer below did solve my problem.