Click to See Complete Forum and Search --> : Need help with making an Array..


Lost_comp
07-23-2003, 08:33 PM
I have 2 make an Array with 2 separate papers I think. I've been at this 4 hours and still can't figure this out.

The first one is(A): Create an arrays of five animals. Use a for loop to display the values stored there. (B): Now add two more animals to the end of the array and sort the array...All I got was a hint 2 use the sort method,display the sorted array...?????:confused: Thanks alot 4 the help in advanced.:D

pyro
07-23-2003, 09:12 PM
Is this a school project? Kinda sounds like it. If so, post your code so far, and we'll give direction... But, if someone were to just give you the code, you won't learn much, would you?

Lost_comp
07-23-2003, 09:19 PM
Not really a school project, but a random scrap hand out that Arrays was never talked about in class, and I just wanted 2 do from the book. Trying 2 be one step ahead.:)

Lost_comp
07-23-2003, 09:28 PM
here's what I have so far.. I don't know where 2 head from there? Like I said teh arrays was never talked about yet.

pyro
07-23-2003, 09:28 PM
Well, if it's not school...

<script type="text/javascript">
animals = new Array("dog","cat","lion","tiger","bear"); //set up the original array
for (i=0; i<animals.length; i++) { //loop through the array
document.write(animals[i]+"<br>"); //write out the values
}
animals.push("horse","cow"); //add two animals to the array
animals.sort(); //sort the array alphabetically.
for (i=0; i<animals.length; i++) { //loop through the new array
document.write(animals[i]+"<br>"); //write the values
}
</script>Please, if you don't understand anything, ask. I'll do my best to explain it to you. As you see, I also commented every line.

Lost_comp
07-23-2003, 09:44 PM
Thanks, I notice that JavaScript is not a easy thing 2 learn,im glad that this site is available 2 people like me, and there's people out that helps out. :)

EDIT: Now I understand how its done.

pyro
07-23-2003, 09:49 PM
Glad I could help you... :)