Java Reference
In-Depth Information
Quiz Ninja Project
Let's use the random() function that we created in this chapter to improve our quiz, so that
the questions are chosen at random rather than just asking them in the order in which they
appear in the array. Our first task is to add the random() function to our function defini-
tions. It can go before or after the play() function:
//// function definitions ////
function random(a,b,callback) {
if(b===undefined) {
// if only one argument is supplied, assume the lower
limit is 1
b = a, a = 1;
var result = Math.floor((b-a+1) * Math.random()) + a;
if(typeof callback === "function") {
result = callback(result);
return result;
Next, we need to update the quiz object that contains the questions so that it includes an
asked property. This will be set to false initially, but will change to true after a ques-
tion has been asked. This enables us to avoid asking the same question twice:
quiz = {
"name":"Super Hero Name Quiz",
"description":"How many super heroes can you name?",
"question":"What is the real name of ",
"questions": [
{ "question": "Superman", "answer": "Clarke Kent", "asked":
false },
{ "question": "Batman", "answer": "Bruce Wayne", "asked":
false },
{ "question": "Wonder Woman", "answer": "Dianna Prince",
Search WWH ::

Custom Search