Prev << >> On to the next chapter: Caesar Cipher.
Can you guess the computer's number? You get five tries:
An overview of the code:
The program is a good example of the while and if statements.
The if statement checks the value of an expression in parentheses.
As you can see in other if statements here, the "else" clause is optional, and if the block of code after an "if" is only a single line, the curly braces may be omitted.
The while statement is similar to the "if" statement: when the expression in parentheses is true, the following block of code is run. After the block is finished, the "while" test is checked again, and as long as it is still true, the block is repeated.
It is possible to break out of a "while" loop without checking the test expression by executing the break statement inside the loop. This causes execution to jump straight out of the loop without checking or looping again. In our program this happens if somebody clicks "Cancel" or enters an empty guess into the prompt.
Javascript works with two atomic types of data: Numbers and Strings.
Even though strings can contain digits, string arithmetic does not work the same as number arithmetic. For example, the following two expressions are both false:
Strings don't do math | Numbers don't concatenate |
'4.0' == '4' is false | 12 == 1 + 2 is false |
However, the following two expressions are both true:
Numbers do math | Strings concatenate |
4.0 == 4 is true | '12' == '1' + '2' is true |
Text input from a user arrives as a string, so if we want to work with a number, we need to convert it by using the Number constructor function:
var n = Number(text);
If the text happens to be not-a-number, then the number is converted to the special numeric value NaN which stands for "Not a Number". You can also get NaN by doing an illegal mathematical operation such as dividing by zero.
Numbers can be compared in all the expected ways. Here is how mathematical comparison operators are written in Javascript:
x == 5 | The equality test operator is written with two equals symbols. |
x != 5 | The inequality test operator is written with an exclaimation point before an equals symbol. |
x < 5 | The strictly-less-than operator looks as you would expect |
x > 5 | The strictly-greater-than operator looks as you would expect |
x <= 5 | Less-than-or-equal-to is written with the equals sign last. |
x >= 5 | Greater-than-or-equal-to is written with the equals sign last again. |
!x | "Not x" is true if x is false, or if x is 0, NaN, the empty string, null, or undefined. |
Do not test equality with a single equals sign, because a single equals sign is only used to assign the value of a variable. (In other words, "x = 5" sets x to 5; it does not test whether x is 5.)
Using a single-equals assignment where a double-equals is needed is probably the single most common error that beginners make in Javascript.
There are a couple odd and old-fashioned things about the Guess-My-Number program:
Try running the program again to see these limitations in action.
Although alert and prompt are old-fashioned and clumsy, they can be terrifically useful to use temporarily when debugging a program. If you are not sure what is going on in a program, you can insert an alert(x) call to announce a message or show the value of a variable at any time.
If you are using a browser with a debugger, console.log(x) can be used to print a message to the debugger's console without blocking the browser. This is even more useful than alert(x), but you need to be aware that console is undefined for browsers without debuggers.
On Chrome you can see the console by type Shift-Control-J. Try it out.
A more modern version of the program plays the game after the whole page is loaded by responding to events when the user submits guesses:
This code is similar to the first example, but the loop has been replaced by an event handler that is called whenever the <input> value has changed:
Using <input> fields instead of prompt and alert boxes makes the interaction part of the webpage so that it does not block the browser whenever we want to ask the user a question. However, writing a program to use <input> instead of prompt is a little bit more difficult and it takes some practice: the difference is that we must write event-based programs.
Event-based programs do the following: