Previous Example-|-Next Example-|-Return to Chapter Listing

Example 9.4:
A Shortcut to Array Creation

Click on the buttons to see what is held in the specified positions in the arrays. This page is the same as Example 9.1, except that we've used an informal method for creating the array. All but the last button will work correctly:


This is the script we used in the HEAD: <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- function makeArray(n) { } var acode = new makeArray(187) var area = new makeArray(187) area[1] = "Alabama "; acode[1] = 205 area[2] = "Alabama "; acode[2] = 334 area[3] = "Alaska "; acode[3] = 907 area[4] = "Alberta "; acode[4] = 403 area[5] = "Antigua "; acode[5] = 268 area[6] = "Arizona "; acode[6] = 520 area[7] = "Arizona "; acode[7] = 602 area[8] = "Arkansas "; acode[8] = 501 area[9] = "Bahamas "; acode[9] = 242 area[10] = "Barbados "; acode[10] = 246 area[11] = "Bermuda "; acode[11] = 441 area[12] = "British Columbia "; acode[12] = 250 area[13] = "British Columbia "; acode[13] = 604 //--> </SCRIPT> And this form: <FORM> <INPUT TYPE="BUTTON" VALUE="What's in area Position 12?" onclick="alert(area[12])"><P> <INPUT TYPE="BUTTON" VALUE="What's in acode Position 12?" onclick="alert(acode[12])"><P> <INPUT TYPE="BUTTON" VALUE="What's in area Position 110?" onclick="alert(area[110])"><P> <INPUT TYPE="BUTTON" VALUE="What's in area Position 500?" onclick="alert(area[500])"><P> <INPUT TYPE="BUTTON" VALUE="What's the area length (area.length)?" onclick="alert(area.length)"><P> </FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing