79Chapter 7Programming Fundamentals, (Web server type) Part IIIf a Web page

79Chapter 7Programming Fundamentals, Part IIIf a Web page included these data tables and a way for a user to look up the entry date for agiven state, the page would need a way to look through all of the USStatesentries to find theindex value of the one that matches the user s entry. Then, that index value could be appliedto the stateEnteredarray to find the matching year. For this demo, the page includes a text entry field in which the user types the name of thestate to look up. In a real application, this methodology is fraught with peril unless the scriptperforms some error checking in case the user makes a mistake. But for now, I assume thatthe user always types a valid state name. (Don t ever make this assumption in your Web site spages.) An event handler from either the text field or a clickable button calls a function thatlooks up the state name, fetches the corresponding entry year, and displays an alert messagewith the information. The function is as follows: function getStateDate() { var selectedState = document.getElementById( entry ).value; for ( var i = 0; i < USStates.length; i++) { if (USStates[i] == selectedState) { break; } } alert( That state entered the Union in + stateEntered[i] + . ); } In the first statement of the function, I grab the value of the text box and assign the value to avariable, selectedState. This is mostly for convenience because I can use the shorter vari- able name later in the script. In fact, the usage of that value is inside a forloop, so the scriptis marginally more efficient because the browser doesn t have to evaluate that long referenceto the text field each time through the loop. The key to this function is in the forloop. Here is where I combine the natural behavior ofincrementing a loop counter with the index values assigned to the two arrays. Specificationsfor the loop indicate that the counter variable, i, is initialized with a value of zero. The loop isdirected to continue as long as the value of iis less than the length of the USStatesarray. Remember that the length of an array is always one more than the index value of the lastitem. Therefore, the last time the loop runs is when iis 50, which is both less than the lengthof 51 and equal to the index value of the last element. Each time after the loop runs, thecounter increments by one. Nested inside the forloop is an ifconstruction. The condition tests the value of an elementof the array against the value typed in by the user. Each time through the loop, the conditiontests a different row of the array starting with row zero. In other words, this ifconstructioncan be performed dozens of times before a match is found, but each time the value of iis onelarger than the previous try. The equality comparison operator (==) is strict when it comes to comparing string values. Such comparisons respect the case of each letter. In our example, the user must type thestate name exactly as it is stored in the USStatesarray for the match to be found. In Chapter10, you learn about some helper methods that eliminate case and sensitivity in stringcomparisons. When a match is found, the statement nested inside the ifconstruction runs. The breakstatement is designed to help control structures bail out if the program needs it. For thisapplication, it is imperative that the forloop stop running when a match for the state name
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services