Java Script Programming

Page 16

Before we move on to new things, this exercise puts the random number function to use in a more complex application. I am also showing you how to use a Java Script file that stores the script you write instead of entering the code as part of the HTML document. On this page, we are creating a test on the states and capitals like the one you see below.

States and Capitals Test

State:  

Capital:  

Correct: Incorrect:

Load your page and see how it looks. Hopefully, it looks the same or similar to the form above. Notice that the text boxes txtState, txtCorrect, and txtIncorrect cannot be focused upon due to the command this.blur() entered into the objects' onFocus event. Notice that the code for the enter calls a function called check() when it is clicked. We write this function momentarily to test the user's answer. The one major difference between this document and the others we have written previously is the opening script tag. Unlike the other pages we have done, this tag reads <Script Language = "JavaScript" src = "StatesCapitals.js">. This tag has an extra assignment. It assigns a value to the src property, which stands for source. The source in this case is StatesCapitals.js. This is the name of the file we are writing momentarily. The .js extension means Java Script, thus creating a Java Script source file. This tag works no differently than an image tag. It just needs to have the proper extension and the code in the .js file needs to be bug-free as usual. Let's get started on this file.

Notice the first line we still use the HTML comment to hide the script from browser's not supporting Java Script. This still works even though this is a Java Script document. So we just started our script with the necessary variables for this program. The variable Correct stores the amount of capitals the user answers correctly, and the variable Incorrect works conversely. WrongGuess is used to see if the user has answered a capital incorrectly. When a capital is guessed incorrectly, this value prevents the user from incrementing his incorrect answers and also tells the user what the correct capital is. Because this application is timed, we use variables Minutes and Seconds to track the time. The variable State is used later to store the random number we generate, which is then used as an array index to test a state and capital answer. The next 3 lines create our arrays. The first two, States and Capitals, store all fifty states and capitals as strings. This is where the form gets the information to display and what to test the user's answer to. Next, the array Answers is used to store the user's entry. These arrays are made up of 50 parts, indexed 0-49.

// store the states in the array
States[0] = "Alabama";
States[1] = "Alaska";
States[2] = "Arizona";
States[3] = "Arkansas";
States[4] = "California";
States[5] = "Colorado";
States[6] = "Connecticut";
States[7] = "Delaware";
States[8] = "Florida";
States[9] = "Georgia";
States[10] = "Hawaii";
States[11] = "Idaho";
States[12] = "Illinois";
States[13] = "Indiana";
States[14] = "Iowa";
States[15] = "Kansas";
States[16] = "Kentucky";
States[17] = "Louisiana";
States[18] = "Maine";
States[19] = "Maryland";
States[20] = "Massachusetts";
States[21] = "Michigan";
States[22] = "Minnesota";
States[23] = "Mississippi";
States[24] = "Missouri";
States[25] = "Montana";
States[26] = "Nebraska";
States[27] = "Nevada";
States[28] = "New Hampshire";
States[29] = "New Jersey";
States[30] = "New Mexico";
States[31] = "New York";
States[32] = "North Carolina";
States[33] = "North Dakota";
States[34] = "Ohio";
States[35] = "Oklahoma";
States[36] = "Oregon";
States[37] = "Pennsylvania";
States[38] = "Rhode Island";
States[39] = "South Carolina";
States[40] = "South Dakota";
States[41] = "Tennessee";
States[42] = "Texas";
States[43] = "Utah";
States[44] = "Vermont";
States[45] = "Virginia";
States[46] = "Washington";
States[47] = "West Virginia";
States[48] = "Wisconsin";
States[49] = "Wyoming";
// store the capitals in the array
Capitals[0] = "Montgomery";
Capitals[1] = "Juneau";
Capitals[2] = "Phoenix";
Capitals[3] = "Little Rock";
Capitals[4] = "Sacramento";
Capitals[5] = "Denver";
Capitals[6] = "Hartford";
Capitals[7] = "Dover";
Capitals[8] = "Tallahassee";
Capitals[9] = "Atlanta";
Capitals[10] = "Honolulu";
Capitals[11] = "Boise";
Capitals[12] = "Springfield";
Capitals[13] = "Indianapolis";
Capitals[14] = "Des Moines";
Capitals[15] = "Topeka";
Capitals[16] = "Frankfort";
Capitals[17] = "Baton Rough";
Capitals[18] = "Augusta";
Capitals[19] = "Annapolis";
Capitals[20] = "Boston";
Capitals[21] = "Lansing";
Capitals[22] = "St. Paul";
Capitals[23] = "Jackson";
Capitals[24] = "Jefferson City";
Capitals[25] = "Helena";
Capitals[26] = "Lincoln";
Capitals[27] = "Carson City";
Capitals[28] = "Concord";
Capitals[29] = "Trenton";
Capitals[30] = "Santa Fe";
Capitals[31] = "Albany";
Capitals[32] = "Raleigh";
Capitals[33] = "Bismarck";
Capitals[34] = "Columbus";
Capitals[35] = "Oklahoma City";
Capitals[36] = "Salem";
Capitals[37] = "Harrisburg";
Capitals[38] = "Providence";
Capitals[39] = "Columbia";
Capitals[40] = "Pierre";
Capitals[41] = "Nashville";
Capitals[42] = "Austin";
Capitals[43] = "Salt Lake City";
Capitals[44] = "Montpelier";
Capitals[45] = "Richmond";
Capitals[46] = "Olympia";
Capitals[47] = "Charleston";
Capitals[48] = "Madison";
Capitals[49] = "Cheyenne";

These lines simply store each state in the States array and each capital in the Capitals array. The other array (Answers) need to remain empty because it stores the user's answer. However, when you use Internet Explorer to run this script, it generates an error when it tests the array for an entry because it does not like null values The next lines avoid this problem.

This code very simply uses the variable index to loop through the array using a for loop. It starts at 0 and ends at 49 because we say index is less than 50. The next command sets the array elements 0 to 49 as a blank string (""). This, however, is not treated as a null (empty) value by the Java Script debugger.

Now let's get this script working.

Line 1: Declare function initialize().

This function is used for the onLoad event of the body to start the test.

Line 3: Set variable State to a random number between 0 and 49. This number is rounded to a whole number using the Math.floor function.
Line 4: Set the focus to the text box txtCapital.
Line 5: Set the text of the text box txtState to the string (text) in array States at the element value state (0 to 49).
Line 6: Set the text in text box txtCorrect to the value of variable Correct.
Line 7: Set the text in text box txtIncorrect to the value of variable Incorrect.
Line 8: Call the function Timer(). We write this function later, which counts the minutes and seconds the user takes to finish.

Now we are going to code the function Timer.

 

On line 14 of the function check (the function we just wrote), a call is made to a function NextState(). Now we are going to write this function.

Line 1: Begin function NextState().
Line 3: Begin Do While loop.

We use the word do here because we want the loop to execute once, even when the test on line 7 is false. A number is generated first without a condition begin true.

Line 5: Set variable State to a random number between 0 and 49, then round it down to a whole number using the Math.floor function. The values generated by Math.random() are between 0 and 1, so the number 50 cannot be generated because the function Math.floor rounds the number down to the nearest whole number.
Line 6: Set the text in text box txtState to the string in array States at the element with value State.
Line 7: Set the focus to the text box txtCapital.
Line 8: Select all the text in the text box txtCapital.
Line 10: Check if the array Answers at index State is equal to the array Capitals at index State. If these are equal, it means that the capital entered into the array index at value State has already been answered. When this happens, line 5 keeps on executing, thus it keeps on creating a random number until it corresponds with an empty array index in the Answers array. The array starts as empty, so this determines if an entry has been made already and prevents a state from being displayed twice. We convert to upper-case because the user may have entered previous answers in lower-case letters, in which case the array index may be displayed again because they are NOT equal.

Hopefully, your form works the same as the one at the top of this page. Now we can continue on to new topics on Page 17.

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11 | Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20 | Page 21 | Page 22 | Page 23 | Page 24 | Page 25

Return Home