Question
 | |  | | | 
Apr 5, 2008, 11:29 AM
| | New Member | | Join Date: Mar 2008
Posts: 20
| | | Javascript Array I need to input a zip code and output a city. I need to fix my javascript code, because first of all I'm getting nothing but code showing up on my page, and second of all, there is no place for the user to input (type) the zip code.
<html>
<title> Javascript Prog </title>
<body bgcolor="green">
<center><h1> What city is the zipcode from?</h1></center>
<body>
<script type="text/javascript">
var zipCode = [78504, 78516, 78537, 78538, 78541, 78557, 78560, 78563,
78570, 78574, 78576, 78577, 78589, 78595, 78596];
var city = ["McAllen","Alamo" ,"Donna" ,"Edcouch" ,"Edinburg"
,"Hidalgo" ,"La Joya" ,"Linn" ,"Mercedes" ,"Mission" ,"Penitas"
,"Pharr" ,"San Juan" ,"Sullivan City" ,"Weslaco"];
zip = prompt("Enter a ZipCode","");
var N = zip.length;
for (var i=0; i < N; i++) {
if (zip == zipCode[i]) {
var j=i; // save position in array
i = N; // terminate loop
}
}
if ( i<= N) alert (city[i]) else alert("city not found"); | | | | | | |
Answers
 | |  | | |
Apr 25, 2008, 11:20 PM
|
#2
| | New Member
Join Date: Apr 2008
Posts: 1
| Hey Hey Smiley,
Just found this sight this evening and happened to see your question here. I pasted your code into my JavaScript editor and I immediately got errors. One reason you were seeing code in your web page is because you did not have the HTML tags in the correct places and some closing elements were missing altogether. All JavaScript (or other languages) need to go inside a <script> tag and be closed with </script>.
Regarding your JavaScript code, you had the basic idea but there were still some errors in your code. I modified your program and pasted it below, adding comments of my own within the program. Here are some more quick comments: - JavaScript is object-based, so when creating arrays, you can use the new Array keywords to create an ARRAY object. Then, you can add values to your array variable (such as zip codes and cities).
- You can use the LENGTH property right in the FOR loop instead of stuffing it into a variable. This cuts down on code (but this is a matter of preference, i'm just pointing this out to you).
- Instead of setting i=N to terminate the FOR loop, you can simply use the BREAK keyword. This will cause the loop to terminate as soon as a condition is true and will then execute the very next statement outside the FOR loop.
- You can use an IF statement to check whether a condition is NOT true by using the NOT operator, which is represented by an !. In my code, I use it to check whether the user's entered zip code does not equal the list of zip codes in the array. If the condition is true (the zip codes do not equal each other), then the alert displays to let the user know that no city is found.
Additionally, it's best to put all actions inside functions to make the code more readable, maintainable and controllable. This isn't necessary right now, but if you begin creating bigger and more complex programs (and you should!), then you'll definitely want to learn more about functions.
These are all just suggestions, a lot of programming is based upon preference and also the rules of the particular organization you are working for. Someone else may comment on my code and improve it further.
Check my revised program here: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title> Javascript Prog </title>
<script type="text/javascript">
//Create new ARRAY using array object to hold zip codes
var zipCode = new Array(78504, 78516, 78537, 78538, 78541, 78557, 78560, 78563, 78570, 78574, 78576, 78577, 78589, 78595, 78596);
//Create new ARRRAY using array object to hold cities
var city = new Array("McAllen", "Alamo" , "Donna" , "Edcouch" , "Edinburg" , "Hidalgo" , "La Joya" , "Linn" , "Mercedes",
"Mission" , "Penitas" , "Pharr" , "San Juan" , "Sullivan City" , "Weslaco");
zip = prompt("Enter a ZipCode","");
for (var i = 0; i < zipCode.length; i++)
{
if (zip == zipCode[i])
{
alert(city[i]); //Messagebox to display the matching city for the zip code entered
break; //JavaScript keyword used to terminate loops - will execute very next statement OUTSIDE of loop
}
}
if (zip != zipCode[i]) //IF statement to evaluate whether entered zipcode DOES NOT equal zipcode from array
{
alert("City not found"); //Messagebox to display if there is no matching city for the zip code entered
}
</script>
</head>
<body>
</body>
</html> |
| | | | | | | | Question Tools | Search this Question | | | | | Display Modes | Linear Mode | |