Java Reference
In-Depth Information
var i = 0;
chooseQuestion();
This sets a variable i to 0 , which will keep track of how many questions have been asked.
We then invoke the chooseQuestion() function. This is a new function that's used to
select the next question; we then ask it by invoking the ask() function. The code for this
function goes with the other nested functions inside the play() function:
function chooseQuestion() {
var question = quiz.questions[i].question;
ask(question);
}
Next, we remove the following from the ask() function, because it uses a prompt dialog
to ask for an answer:
return prompt("Enter your answer:");
We'll replace it with a couple of lines that give focus to the form's input field and also re-
move any previous answer. The input field is the first element in the form, so has an index
of 0 :
$form[0].value = "";
$form[0].focus();
We need to wait for the player to enter an answer and submit the form before we check
the answer. We can use an event listener to check when the form has been submitted
and then invoke the check() function. Add the following code to the beginning of the
play() function—it has to go inside the play() function so that it can invoke the nes-
ted check() function:
$form.addEventListener('submit', function(event) {
event.preventDefault();
check($form[0].value);
}, false);
Search WWH ::




Custom Search