Java Reference
In-Depth Information
document.write(“<br />Execution continues here”);
</script>
</body>
</html>
Load this into your browser and enter, for example, the value 1 in the prompt box. You should then see
something like what is shown in Figure 3-8.
Too low!
Execution continues here
Figure 3-8
If, on the other hand, you enter the value 3, you should see a friendly message letting you know that
you guessed the secret number correctly, as shown in Figure 3-9.
You guessed the secret number!
Execution continues here
Figure 3-9
First you declare the variable secretNumber and set it to the value entered by the user via the prompt
box. Note that you use the parseInt() function to convert the string that is returned from prompt()
to an integer value.
var secretNumber = prompt(“Pick a number between 1 and 5:”, “”);
secretNumber = parseInt(secretNumber);
Next you create the start of the switch statement.
switch (secretNumber)
{
The expression in parentheses is simply the variable secretNumber, and it's this number that the case
statements will be compared against.
You specify the block of code encompassing the case statements using curly braces. Each case state-
ment checks one of the numbers between 1 and 5 , because this is what you have specifi ed to the user
that she should enter. The fi rst simply outputs a message that the number she has entered is too low.
case 1:
document.write(“Too low!”);
break;
The second case statement, for the value 2, has the same message, so the code is not repeated here. The
third case statement lets the user know that she has guessed correctly.
case 3:
document.write(“You guessed the secret number!”);
break;
Search WWH ::




Custom Search