HTML and CSS Reference
In-Depth Information
You can use a similar approach to get whole numbers in any range. For example, if you want the numbers 1
to 13, youd multiply the random number by 13 and then add 1. This could be useful for a card game. Youll
see similar examples throughout this topic.
We can combine all of these steps together into what is called an expression . Expressions are
combinations of constants, methods, and function calls, and some things well explore later. We put these
items together using operators, such as + for addition and * for multiplication.
Remember from Chapter 1 how tags can be combined—nesting a tag within another tag—and the one line
of JavaScript code we used in the Favorite Sites application:
document.write(Date());
We can use a similar process here. Instead of having to write the random call and then the floor method
as separate statements, we can pass the random call as an argument of the floor method. Take a look at
this code fragment:
1+Math.floor(Math.random()*6)
This expression will produce a number from 1 to 6. I call it a code fragment because it isn't quite a
statement. The operators + and * refer to the arithmetic operations and are the same as youd use in
normal math. The order of operations starts from the inside and works out.
Invoke Math.random() to get a decimal number from 0 up to, but not quite 1.
Multiply the result by 6.
Take that and strip away the fraction, leaving the whole number, using Math.floor .
Add 1.
Youll see a statement with this expression in our final code, but we need to cover a few other things first.
Variables and assignment statements
Like other programming languages, JavaScript has a construct called a variable, which is essentially a
place to put a value, such as a number. It is a way of associating a name with a value. You can use the
value later by referencing the name. One analogy is to office holders. In the USA, we speak of “the
president.” Now, in 2010, the president is Barack Obama. Before January 21, 2009, it was George W.
Bush. The value held by the term “the president” changes. In programming, the value of the variable can
vary as well, hence the name.
The term var is used to declare a variable.
The names of variables and functions, described in the next section, are up to the programmer. There are
rules: no internal blanks and the name must start with an alphabetic character. Don't make the names too
long as you don't want to type too much, but don't make them so short you forget what they are. You do
need to be consistent, but you don't need to obey the rules of English spelling. For example, if you want to
set up a variable to hold the sum of values and you believe that sum is spelled som, thats fine. Just make
sure you use som all the time. But if you want to refer to something thats a part of JavaScript, such as
function or document or random , you need to use the spelling that JavaScript expects.
You should avoid using the names of built-in constructs in JavaScript (such as random or floor ) for your
variables. Try to make the names unique, but still easily understandable. One common method of writing
variable names is to use whats called camel case. This involves starting your variable name in lower
case, then using a capital letter to denote when a new word starts, for example, numberOfTurns or
 
Search WWH ::




Custom Search