HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [140]
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
As you can see, I converted the strategy from the previous section directly into JavaScript code:
1. Create a random float.
The Math.random() function creates a random floating-point number and stores it in the variable number.
2. Multiply the number by 6 to move the number into the appropriate range (6 values).
I multiplied by 6 and stored the result in biggerNumber.
3. Round up.
I used the Math.ceil() function to round the number up to the next highest integer.
Figure 2-1 shows the program running.
Figure 2-1: This program generates a value between 1 and 6.
You may need to run the rollDice.html page a few times to confirm that it works as suspected.
If you want to rerun a program you’ve loaded into the browser, just hit the page refresh button on the browser toolbar.
Using if to Control Flow
If you can roll a die, you’ll eventually want different things to happen based on the results of the die roll. Figure 2-2 shows two different runs of a simple game called deuce.html.
Okay, it’s not that exciting. I promise to add dancing hippos in a later version.
In any case, the “You got a Deuce!” message happens only when you roll a 2. The code is simple but profound:
As usual, I’m showing only the script tag and its contents here, because the rest of the page is blank.
Figure 2-2: Something exciting happens when you roll a two.
The basic if statement
The key to deuce.html is the humble if statement. This powerful command does a number of important things:
♦ It sets up a condition. The main idea behind a condition is that it’s a true or false question. An if statement always includes some type of condition in parentheses. (For more on conditions, see the next section.)
♦ It begins a block of code. An if statement sets up a chunk of code that doesn’t always execute. The end of the if line includes a left brace ({).
♦ It usually has indented code under it. The line or lines immediately after the if statement are part of the block, so they’re indented to indicate that they’re special.
♦ It ends several lines later. The end of the if statement is actually the right brace (}) several lines down in the code. In essence, an if statement contains other code.
♦ It’s indented. The convention is to indent all the code between the if statement and its ending brace.
Although not required, many programmers add a comment to indicate that the right brace ends an if statement. In the C-like languages, the same symbol (}) is used to end a bunch of things, so it’s nice to remind yourself what you think you’re ending here.
All about conditions
A condition is the central part of if and several other important structures. Conditions deserve a little respect of their own. A condition is an expression that can be evaluated to true or false. Conditions come in three flavors:
♦ Comparison: By far the most common kind of condition. Typically, you compare a variable to a value, or two variables to each other. Table 2-1 describes a number of different types of comparisons.
♦ Boolean variable: A variable that contains only true or false. In JavaScript, any variable can be a Boolean, if you assign it true or false as a value.