Java Reference
In-Depth Information
Quiz Ninja Project
Now that we have come to the end of the chapter, it's time to put what we've learned into
practice in our Quiz Ninja project.
Since we've been learning all about JavaScript in this chapter, we're going to add some code
in the scripts.js file. Open that file and add the following lines:
scripts.js (excerpt)
var question = "What is Superman's real name?"
var answer = prompt(question);
alert("You answered " + answer);
Now let's go through this code line by line to see what is happening:
var question = "What is Superman's real name?"
This declares a variable called name and assigns the string "What is Superman's
real name?" to it. Next, we need to ask the question stored in the question variable,
using a prompt dialog:
var answer = prompt(question);
A prompt dialog allows the player to type in an answer, which is stored in a variable called
answer .
Finally, we use an alert dialog to display the player's answer:
alert("You answered " + answer);
This shows the player the answer they provided. In the next chapter we'll look at how to
check if it's correct.
Have a go at playing the quiz by opening the index.htm file in your browser. It should look
a little like the screenshot in Figure 2.1 .
Search WWH ::




Custom Search