Java Reference
In-Depth Information
This will update the score element to display a score of 0 in the header.
The ask() function now needs an extra line so that the question is displayed in the HTML
instead of the prompt dialog. Unfortunately we still need to use a prompt dialog to enter the
answer (but don't worry, this will change in later chapters!):
scripts.js (excerpt)
function ask(question) {
update($question,quiz.question + question);
return prompt("Enter your answer:");
This will add a paragraph element to the question section that contains the question, with a
prompt dialog asking for the answer.
Next we need to change the check() function so that it provides some feedback and up-
dates the score element:
scripts.js (excerpt)
function check(answer) {
if(answer === quiz.questions[i].answer){
// increase score by 1
} else {
Notice that the updates to the feedback element also contain a third argument of right or
wrong that will be added as a class to the section. This means that we can style the feed-
back differently, depending on whether the player's answer is right or wrong.
Search WWH ::

Custom Search