JQuery multiple-choice quiz script: how to ensure the correct answer is available?
- by DavidR
I have a function. I pass to it an object with a list of questions and answers. I generate a random number from a range equal to the number of questions in the object. The script check to see if that question has been asked, and if not, it asks it, the correct answer is then passed to a variable correct. I want to give 4 possible answers, each one randomly selected from the list of possible answers. I have this. I just want to know what is the best way to ensure that the correct answer is placed in the list.
So far, this is what I have: it puts four answers into four targeted boxes. I want to ensure that the correct answer is in a randomly selected box.
function getRandom(limit) {
return Math.floor(Math.random() * limit )
}
$("#answers > .answer").each(function(i){
r = getRandom(total_questions);
$(this).text(answers[r])
});