JQuery multiple-choice quiz script: how to ensure the correct answer is available?

Posted by DavidR on Stack Overflow See other posts from Stack Overflow or by DavidR
Published on 2011-01-01T20:37:17Z Indexed on 2011/01/01 20:54 UTC
Read the original article Hit count: 190

Filed under:
|

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])
});

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about education