HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [157]
The following code shows a basic demonstration of arrays:
The variable genre is a special variable because it contains many values. Essentially, it’s a list of genres. The new array(5) construct creates space in memory for five variables, all named genre.
Accessing array data
After you specify an array, you can work with the individual elements using square-bracket syntax. An integer identifies each element of the array. The index usually begins with zero.
genre[0] = “flight simulation”;
The preceding code assigns the text value “flight simulation” to the genre array variable at position 0.
Most languages require all array elements to be the same type. JavaScript is very forgiving. You can combine all kinds of stuff in a JavaScript array. This flexibility can sometimes be useful, but be aware that this trick doesn’t work in all languages. Generally, I try to keep all the members of an array the same type.
After you store the data in the array, you can use the same square-bracket syntax to read the information.
The line
alert (“I like “ + genre[4] + “ games.”);
finds element 4 of the genre array and includes it in an output message.
Figure 4-5 shows a run of genre.html.
Figure 4-5: This data came from an array.
Using arrays with for loops
The main reason to use arrays is convenience. When you have a lot of information in an array, you can write code to work with the data quickly. Whenever you have an array of data, you commonly want to do something with each element in the array. Take a look at games.html to see how you can do so:
Notice several things in this code:
♦ The array called gameList. This array contains the names of some of my favorite freeware games.
♦ The array is preloaded with values. If you provide a list of values when creating an array, JavaScript simply preloads the array with the values you indicate. You don’t need to specify the size of the array if you preload it.
♦ A for loop steps through the array. Arrays and for loops are natural companions. The for loop steps through each element of the array.
♦ The array’s length is used in the for loop condition. Rather than specifying the value 10, I used the array’s length property in my for loop. This practice is good because the loop automatically adjusts to the size of the array when I add or remove elements.
♦ Do something with each element. Because i goes from 0 to 9 (the array indices), I can easily print each value of the array. In this example, I simply add to an output string.
♦ Note the newline characters. The \n combination is a special character that tells JavaScript to add a carriage return, such as pressing the Enter key. Figure 4-6 shows a run of games.html.
Figure 4-6: Now I have a list of games. Arrays and loops are fun!
If you want to completely ruin your productivity, Google some of these game names. They’re absolutely incredible, and every one of them is free. It’s hard to beat that. See, even if you don’t learn how to program in this book, you get something good from it!
Revisiting the ants song
If you read the earlier sections, you probably just got that marching ant