HTML and CSS Reference
In-Depth Information
["dish",10.50],
["rug",599.99]
];
This store has 4 items, with the cheapest being the dish, represented in the position at index 2, and the
most expensive the rug at index 3.
Now, let's see how we can use these concepts for defining a gradient. Well use an array whose individual
elements are also arrays.
Each inner array holds the RGB values for a color, namely red, yellow, green, cyan, blue, magenta.
var hue = [
[255, 0, 0 ],
[255, 255, 0 ],
[ 0, 255, 0 ],
[ 0, 255, 255 ],
[ 0, 0, 255 ],
[255, 0, 255 ]
] ;
These values represent colors ranging from red (RGB 255,0,0) to magenta (RGB 255,0,255), with four
colors specified in between. The gradient feature in JavaScript fills in the colors to produce the rainbow
pattern shown in Figure 3-3. Gradients are defined by specifying points along an interval from 0 to 1. You
can specify a gradient other than a rainbow. For example, you can use a graphics program to select a set
of RGB values to be the so-called stop-points, and JavaScript will fill in values to blend from one to the
next.
The array numeric values are not quite what we need, so we will have to manipulate them to produce what
JavaScript demands.
Manipulation of arrays often requires doing something to each member of the array. One construct for
doing this, present in many programming languages, is the for loop, which uses a variable called an
indexing variable. The structure of the for loop is
for (initial value for indexing variable; condition for continuing; change for
indexing variable) {
code to be done every time. The code usually references the indexing variable
}
This says: start with this initial value; keep doing the loop as long as this condition holds; and change the
index value in this specified way. A typical expression for the change will use operators such as ++ . The
++ operator increments the indicated variable by 1. A typical for header statement is
for (n=0;n<10;n++)
This for loop uses a variable named n , with n initialized to 0. If the value of n is less than 10, the
statements inside the loop are executed. After each iteration, the value of n is increased by 1. In this
case, the loop code will be executed 10 times, with n holding values 0, 1, 2, all the way up to 9.
Heres one more example, a common one to demonstrate arrays. Let the grades variable be set up to hold
a set of grades for a student:
var grades = [4.0, 3.7, 3, 2.3, 3];
 
Search WWH ::




Custom Search