HTML and CSS Reference
In-Depth Information
Gradients involve sets of colors. A typical practice is to write code to set what are called the color stops,
such as to make the gradient be a rainbow. For this, I set up an array of arrays in a variable named hue .
You can think of an array as a holder for a collection of values. Whereas a variable can hold only one
value, an array can hold many. In the next chapter, youll read about an array named everything that will
hold all the objects to be drawn on the screen. In Chapter 9, which describes the Hangman game, the word
list is an array of words. Youll read about many applications of arrays in this topic. Heres a concrete
example. The following var statement sets up a variable to be a specific array:
var family = ["Daniel","Aviva", "Allison", "Grant", "Liam"];
The variable family is an array. Its data type is array. It consists of a list of people in my family (for
pictures, see the memory game described in Chapter 5). To access or to set the first element of this array,
youd use family[0] . The values to specify specific members of an array are called index values or
indices. Array indexing starts with zero. The expression family[0] would produce Daniel . The
expression family[4] would produce Liam . If the value of a variable relative was 2, then
family[relative] would produce Allison . To determine the number of elements in the array, youd use
family.length . In this case, the length is 5.
The individual items in an array can be of any type, including arrays. For example, I could modify the family
array to provide more information:
var family = [["Daniel","college teacher"],
["Aviva", "congressional staff"],
["Allison","graduate student"],
["Grant","kid"],
["Liam","kid"]
];
The formatting, with the line breaks and indents, is not required, but its good practice.
The expression family[2][1] produces "graduate student". Remember: array indexing starts at 0 so the
index value 2 for the array, sometimes termed the outer array in this type of example, produces
["Allison","graduate student"] and the array 1, the index for the inner array, produces "graduate student".
These inner arrays do not have to be the same length. Consider the example:
var family = [["Daniel","college teacher"],
["Aviva", "congressional staff"],
["Allison","graduate student"],
["Grant"],
["Liam"]
];
The code would check the length of the array and if it was 2 instead of 1, the second item would be the
profession of the individual. If the length of the inner array was 1, it would be assumed that the individual
does not have a profession.
Arrays of arrays can be very useful for product names and costs. The following statement specifies the
very limited inventory of a store:
var inventory = [
["toaster",25.99],
["blender",74.99],
 
Search WWH ::




Custom Search