HTML and CSS Reference
In-Depth Information
userFirstThrow. You can see why its called camel case—the capitals form “humps” in the word. You dont
have to use this naming method, but its a convention many programmers follow.
The line of code that will hold the pseudo-random expression explained in the previous section is a
particular type of statement called an assignment statement. For example,
var ch = 1+Math.floor(Math.random()*);
sets the variable named ch to the value that is the result of the expression on the right-hand side of the
equal sign. When used in a var statement, it also would be termed an initialization statement. The =
symbol is used for setting initial values for variables as in this situation and in the assignment statements
to be described next. I chose to use the name ch as shorthand for choice. This is meaningful for me. In
general, though, if you need to choose between a short name and a longer one that you will remember,
pick the longer one! Notice that the statement ends with a semi-colon. You may ask, why not a period?
The answer is that a period is used in two other situations: as a decimal point and for accessing methods
and properties of objects, as in document.write .
Assignment statements are the most common type of statements in programming. Heres an example of
an assignment statement for a variable already defined:
bookname = "The Essential Guide to HTML5";
The use of the equal sign may be confusing. Think of it as making it true that the left-hand side equals
whats produced by the right-hand side. Youll encounter many other variables and other uses of operators
and assignment statements in this topic.
Caution: The var statement defining a variable is called a declaration statement. JavaScript, unlike
many other languages, allows programmers to omit declaration statements and just start to use a
variable. I try to avoid doing that, but you will see it in many online examples.
For the game of craps, we need variables that define the state of the game, namely whether it is a first
throw or a follow-up throw, and what the player's point is (remember that the point is the value of the
previous throw). In our implementation, these values will be held by so-called global variables , variables
defined with var statements outside of any function definition so as to retain their value (the values of
variables declared inside of functions disappear when the function stops executing).
You dont always need to use variables. For example, the first application we create here sets up variables
to hold the horizontal and vertical position of the dice. I could have put literal numbers in the code because
I don't change these numbers, but since I refer to these values in several different places, storing the
values in variables mean that if I want to change one or both, I only need to make the change in one place.
Programmer-defined functions
JavaScript has many built-in functions and methods, but it doesnt have everything you might need. For
example, as far as I know, it does not have functions specifically for simulating the throwing of dice. So
JavaScript lets us define and use our own functions. These functions can take arguments , like the
Math.floor method, or not, like Math.random . Arguments are values that may be passed to the function.
Think of them as extra information. The format for a function definition is the term function followed by
the name you want to give the function, followed by parentheses holding the names of any arguments,
followed by an open bracket, some code, and then a closed bracket. As I note in the previous sections,
 
Search WWH ::




Custom Search