Java Reference
In-Depth Information
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.
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, 10);
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
statement checks one of the numbers between 1 and 5 , because this is what you have specified to the
user that she should enter. The first 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