Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays 1

Status
Not open for further replies.

wuzzle

Programmer
Dec 20, 2000
75
CA
Can someone tell me what this code is doing, from the "for" statement down.

birthdayarray = new Array(12);
birthdayarray[0] = "January";
birthdayarray[1] = "January";
birthdayarray[2] = "February";
birthdayarray[3] = "March";
birthdayarray[4] = "April";
birthdayarray[5] = "May";
birthdayarray[6] = "June";
birthdayarray[7] = "July";
birthdayarray[8] = "August";
birthdayarray[9] = "September";
birthdayarray[10] = "October";
birthdayarray[11] = "November";
birthdayarray[12] = "December";

var searchstring = prompt("What number of month were you born in?","");

for (m=0; m<birthdayarray.length; m++){
alert(m);
if (birthdayarray[m].search(searchstring) != -1){
alert(&quot;You were born in &quot;+birthdayarray[m]+&quot;, which is month &quot;+m+&quot; out of 12.&quot;)
}
}
 
It gets the entered text at the prompt and then goes thru the array of birthdays.

At every iteration the array entry is comparing itself to the searchstring - and if this does not match then -1 is returned.

so if the string i the array[m] entry is the same as the onegiven as 'searchstring' - the alert message is given.

So the search is just a pattern matching device.
b2 - benbiddington@surf4nix.com
 
Thank you so much, that makes it alot easier to understand. Now how would I make it so that after they enter their number, the alert box comes up and tells them what month it corresponds to, without having to count to 12?
 
replace the alert at the end with:


alert(&quot;You were born in &quot;+ birthdayarray[m]);


This is what was displayed though - but with that other stuff was it not? It should ahve already given the month because that's what birthdayarry[m] holds.

Also get rid of the first alert(m) - if you haven't already.
maybe that's what you were meaning? In actual fact yes I think that is what you meant, you just don't want the counter!
b2 - benbiddington@surf4nix.com
 
That's what I meant - I just had to get rid of alert(m). It works perfectly now! Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top