Ask Experts Questions for FREE Help!
  Advanced
Register  |  Log in  
   Ask    
 Answer  
  Help  

Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
Free Answers in 3 Easy Steps

Register Now
3 Steps

At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.

Home > Computers & Technology > Programming > Scripting > Javascript   »   Javascript Array

 
Question Tools Search this Question Display Modes
Question
 
 
#1  
Old Apr 5, 2008, 11:29 AM
smileyman22336
New Member
smileyman22336 is offline
 
Join Date: Mar 2008
Posts: 20
smileyman22336 See this member's comment history on his/her Profile page.
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");

Reply With Quote
 
     

Answers
 
 
Old Apr 25, 2008, 11:20 PM   #2  
Avenue
New Member
Avenue is offline
 
Join Date: Apr 2008
Posts: 1
Avenue See this member's comment history on his/her Profile page.
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>
  Reply With Quote
 
     


Question Tools Search this Question
Search this Question:

Advanced Search
Display Modes

 
Similar Sponsors

Similar Questions
Question Asker Topic Answers Last Post
array formula ptbuster Spreadsheets 12 Mar 15, 2008 05:20 AM
array controller davbrew2005 Hard Drives 1 Mar 7, 2008 07:07 AM
Unknown Array Java Error Darkrune495 Java 3 Feb 29, 2008 10:00 AM
an array Crispy C++ 1 Oct 15, 2007 09:12 AM
code to define array lzardoya Java 1 May 16, 2007 04:37 PM




Copyright ©2003 - 2007, Ask Me Help Desk.
All times are GMT -8. The time now is 09:40 PM.

Content Relevant URLs by vBSEO 3.0.0 RC6 © 2006, Crawlability, Inc.