PDA

View Full Version : How to Use Arrays to build quiz


saimungi
12-07-2003, 11:50 AM
I am trying to make a small quiz programme as my first project, wherein 10 questions are asked and if you can answer 6 of them you are taken to second round. Using two buttons ans_right and ans_wrong. How to place the set of questions and answers in random sequence.

Thank you !

Uday

xxlm
12-07-2003, 11:29 PM
This question have been asked lots of time already...
But...

Just create an array and put your question/answer in

var myQuestion = new Array();
var myAnswer = new Array();
var myQuiz = new Array();

//put the question in the myQuestion array
myQuestion.push("yourquestion 1");
myQuestion.push("yourquestion 2");
...
myQuestion.push("yourquestion n");

//put the answer in the myAnswer array
myAnswer.push("yourAnswer 1");
myAnswer.push("yourAnswer 2");
...
myAnswer.push("yourAnswer n");

//making 2D array
myQuiz.push(myQuestion);
myQuiz.push(myAnswer);

while (myQuiz[0].length != 0) {
rdm = Math.random() * (myQuiz[0].length - 1);
yourQuestion = myQuiz[0][rdm];
yourAnswer = myQuiz[1][rdm];
/*
here you're doing your stuff like display and so on
*/

//removing the question asked already
myQuiz[0].splice(rdm, 1);
myQuiz[1].splice(rdm, 1);
}



I don't test it.. I wrote it as is on the board so test it... It should be ok...

Cya :) :)