View Full Version : Animal Array...
ecntrc
07-23-2003, 11:19 PM
heres the problem..
create an array of five animals. use a for loop to display the values stored there. now add two more animals at the end of the array and sort the array. he says use the sort() method then he wants us to display the sorted array...
heres what i have, and i have a feeling that it is not correct,
<html>
<body>
<script language="_JavaScript">
function AnimalArray(length) {
var i = 0
this.length2 = length
for (i=0; i<length; i++)
this[i] = 0
return this
}
function AddAnimal(AnimalName) {
this.AnimalName = AnimalName
return this
}
var totAnimals = 5
var Animals = AnimalArray(totAnimals)
Animals[0] = new AddAnimal("Tiger")
Animals[1] = new AddAnimal("Snake")
Animals[2] = new AddAnimal("Lion")
Animals[3] = new AddAnimal("Birds")
Animals[4] = new AddAnimal("Bear")
Animals[5] = new AddAnimal("Monkey")
</script>
</body>
</html>
any help or suggestions?
beetle
07-24-2003, 12:44 AM
Is this schoolwork?
ecntrc
07-24-2003, 12:59 AM
yes it is schoolwork, dont tell me to look in the book because thats where i got the code from. well not got the code from but i used examples from the book. i read the book..is there something wrong?
scriptkeeper
07-24-2003, 01:18 AM
http://www.codingforums.com/showthread.php?s=&threadid=2090
Read Number 3 Please!
Read it all if you havent read yet!
ecntrc
07-24-2003, 01:48 AM
OK YOU KNOW WHAT, IM NOT ASKING SOMEONE TO DO MY HOMEWORK ALRIGHT. I ASKED FOR SUGGESTIONS OR ANY HELP. SO YOU KNOW WHAT, I WONT BE USING THIS SERVICE ANYMORE. THANKS FOR THE HELP THAT I DIDNT RECIEVE LIKE I ASKED...
scriptkeeper
07-24-2003, 01:56 AM
Sorry I didn't mean it like that! Just meant that you should ask a more specific question as to fix a certain error like "I think the problem is in this line!" Or "I get this error when I run it!" But in your post it said "heres what i have, and i have a feeling that it is not correct" as if you have not even tested it yet! I am sorry if I came of abrasive I didn't mean to scare you off from these forums!
ecntrc
07-24-2003, 03:53 AM
no thats the second time that something like this has happened, actually the third. and when i run the code nothing shows up on my browser. so i dont know where the problem is, ive read the posting requirements, thats the very first thing i did when i saw this site..
beetle
07-24-2003, 04:01 AM
What are you expecting to happen? I see no alert()s, no document.write() commands - no output functions whatsoever.
Your script runs - you just don't have any output!
I'll give you some hints
<html>
<body>
<script language="JavaScript">
// Create regular array with 5 values
var animals = new Array( "Horse", "Cow", "Chicken", "Pig", "Sheep" );
for ( var i = 0; i < animals.length; i++ )
{
// output animal name to page here
}
// Add two new animals to array here
// Sort the array
// Reuse the same loop from above to output the array again
</script>
</body>
</html>
arnyinc
07-24-2003, 01:57 PM
Here's some more hints.
Create a global array with 5 elements
var animals = new Array("Horse", "Cow", "Chicken", "Pig", "Sheep");
Create a function to print out your array. All you need is a for loop that cycles through and prints out each element.
function DisplayArray(){
for(loopctr=0; loopctr<animals.length; loopctr++)
document.write(animals[loopctr]+"<br>");
}
Create a function to add an animal to the array.
function AddAnimal(myanimal){
animals[animals.length]=myanimal;
}
Then call these functions.
DisplayArray();
AddAnimal("Rooster");
AddAnimal("Goat");
animals.sort();
DisplayArray();
You might want to pass a parameter to the DisplayArray function to denote what it is printing (i.e. printing the unsorted array, then the sorted array) or you might just want to put some break in between.
brothercake
07-24-2003, 06:07 PM
arnyinc - I think you've posted more than a hint there ... I know you mean well, but you really don't do students any favours by giving them assignment answers so readily.
newburd42o - I know you only wanted help and not a complete answer, but people weren't necessarily to know that - when asking for this kind of help it's really useful to give context - mention what you've already tried, what you think might be the problem .. "show your workings", as the teachers always used to say to me.
And please don't type in all capitals; it's inferred as shouting.
joeframbach
07-24-2003, 06:59 PM
imho, i wouldve shouted at scriptkeeper too. newburd asked for help or suggestions, she didnt ask for the assignment to be done.
scriptkeeper
07-24-2003, 07:08 PM
Shout Away! Like BrotherCake said:
when asking for this kind of help it's really useful to give context - mention what you've already tried, what you think might be the problem .. "show your workings", as the teachers always used to say to me.
I was only trying to help he/she to post a more descriptive and helpfull question if that makes me a bad person then call me saddam hussien!
joeframbach
07-24-2003, 07:12 PM
we're getting too off topic.
newburd, there is an underscore in the script tag that shouldnt be there :D
arnyinc
07-24-2003, 07:34 PM
Originally posted by joeframbach
there is an underscore in the script tag that shouldnt be there :D
That confused the bejesus out of me when I copied and pasted that into my script. :confused:
brothercake
07-24-2003, 08:15 PM
To all concerned, just remember not to make assumptions - don't assume you know what other people's motivations are, or why a question was asked in a particular way, and most importantly - if you read something that feels like its written in an aggressive or dodgy tone, please refrain from responding in a similarly aggressive tone ... it just increases the overall amount of bad vibes on the board, and none of us want that.
joeframbach
07-25-2003, 02:11 AM
a wise man one said "we dont want anybody dying, because that leads to paperwork, and nobody likes paperwork"
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.