1

Hey Guys my goal for this multi dimensional array is to document the answers (right or wrong) for a test that the user is taking. In the beginning the test array is set up. as the user goes throught the questions the app should add to the array. Note: the page doesnt reload, the questions come via ajax.

Here is what i have so far

   var test = new array();

   function nextquestion(){
      var result;

    if (document.getElementById('rb2').checked = true){
  result = 'correct' ;
    }
else{

result = 'incorrect';
}
var num = document.getElementByName('rb').value;

var question =new Array(); 
question1[0]= document.getElementByName('rb').value; //question id    
question1[1]= result;

I want the test array to hold the data like so

   test[0]
  test[1][0] = question 1
  test[1][1] = correct
  test[2][0] = question 4
  test[2][1] = incorrect
  test[3][0] = question 17
  test[3][1] = correct
  test[4][0] = question 12
  test[4][1] = incorrect



  test[0]
  test[1][0] = question 1
  test[1][1] = correct
  test[2][0] = question 2
  test[2][1] = incorrect
  test[3][0] = question 3
  test[3][1] = correct
  test[4][0] = question 4
  test[4][1] = incorrect

also what must i add to the next function to get the value of the question id so that i can + 1 to get the next question from the database

thanks in advance!!

1
  • 1
    if (document.getElementById('rb2').checked = true) is a problem: don't say .checked = true (assignment), say .checked === true (comparison). Or, given that the .checked property is a boolean you don't need the comparison at all: if (document.getElementById('rb2').checked) {}. Also the first line of your code should reference Array() with a capital "A", or, better: var test = []. Commented Nov 28, 2011 at 5:12

1 Answer 1

2

If you want elements of your test array to hold other arrays, you have to set that up manually:

var test = [];
test.push([]);
test[0].push("Question 1");
test[0].push(true); //or "correct"

var q1 = test[0][0];
//"Question 1"

var wasQ1Correct = test[0][1];
//true

And so on

Sign up to request clarification or add additional context in comments.

Comments

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.