Click to See Complete Forum and Search --> : my variable is not recognised as one.
omelette
11-12-2003, 02:42 AM
I want to get a string to pass into a function as a variable. For example, I have:
zoo_list = new Array("giraffe", "lion", "tiger", "hippo");
hippo = new Array("Hippogirl", "Hippee");
function myAnimal (trait) {
DoWhatever
}
THIS WORKS:
It works if I called the function like so:
myAnimal(Hippogirl);
THIS DOESN'T:
When written like this, it doesn't work:
for (i=0; i=hippo.length; i++) {
myAnimal(hippo[i]);
}
When looped, "Hippogirl", etc... aren't passed in as a variable. I hope someone understands what I'm trying to do.
skriptor
11-12-2003, 03:20 AM
Hi,
your for-statement is wrong. Try something like this
for (i=0; i<hippo.length; i++) { }
Good Luck, skriptor
Scriptage
11-12-2003, 05:30 AM
zoo_list = new Array("giraffe", "lion", "tiger", "hippo");
hippo = new Array("Hippogirl", "Hippee");
are you trying to do an array inside an array? if so:
...
hippo = new Array("Hippogirl", "Hippee");
zoo_list = new Array(giraffe, lion, tiger, hippo);
...
omelette
11-12-2003, 09:10 PM
Thanks Skriptor & Scriptage,
Sorry Skriptor, I made a typo error - my real for loop works. But thanks for helping a javascript-newbie like me :) anyway.
Scriptage, that's exactly what I'm trying to do! It works now! I hadn't realised my variables were the wrong way round too! Thanks a bundle! Omelette grateful...
Omelette makes some Script-Omelette and posts it to Skriptor & Scriptage for brekky! :D
omelette
11-13-2003, 02:23 AM
Sorry but I ran into another problem.
I want to write:
document.write("The names of my " + zoo_list[3] + "s are " + hippo);
but zoo_list[3] keeps becoming "Hippogirl,Hippee" when I want it just write "hippo".
Pittimann
11-13-2003, 02:33 AM
Hi omelette!
Do you want this to be put out:
The names of my hippos are Hippogirl,Hippee ???
omelette
11-13-2003, 06:56 PM
Sorry, I guess I didn't make myself clear. Yeah! I wanted the output to be:
"The names of my hippos are Hippogirl,Hippee". Is it possible?
This is what I've written so far:
lion = new Array("Lyan", "Grrr");
hippo = new Array("Hippogirl", "Hippee");
zoo_list = new Array(lion, hippo);
for (i=0; i<zoo_list.length; i++) {
document.write("The names of my " + zoo_list[i] + " are " + zoo_list[i] + "<br>");
}
Pittimann
11-13-2003, 11:11 PM
Hi omelette!
Of course this is possible. Here is some code which also checks singular and plural:
<script language="JavaScript" type="text/javascript">
//Array for the names of species in the zoo:
zoo_list = new Array("giraffe", "lion", "tiger", "hippo");
//
//Array for the names of animals of every species:
hippo = new Array("Hippomom","Hippodad","Hippogirl","Hippoboy", "Hippoaunt");
lion = new Array("Clarence", " Elsa");
tiger = new Array("Shir Khan");
giraffe = new Array("Long Neck");
//
//Array containing the arrays with the individual names:
zoo_list_array = new Array(giraffe, lion, tiger, hippo);
//
//listing the species and their number of individuals
//only 1 species in zoo
if (zoo_list_array.length==1){
if (zoo_list_array[0].length==1) animal=zoo_list[0];//only 1 individual of that species
else animal=zoo_list[0]+"s";//more than 1 individual of that species
document.write("In my zoo I've got "+zoo_list_array[0].length+" "+animal+".<br><br>");
}
//
//2 species in zoo
if (zoo_list_array.length==2){
document.write("In my zoo I've got ");
for (i=0; i<zoo_list_array.length; i++) {//loop for 2 species
if (zoo_list_array[i].length==1) animal=zoo_list[i];//1 individual
else animal=zoo_list[i]+"s";//more than 1 individual
if (i==0) document.write(zoo_list_array[i].length+" "+animal+" and ");
if (i==1) document.write(zoo_list_array[i].length+" "+animal+".");
}
document.write("<br><br>");
}
//
//more than 2 species in zoo
if (zoo_list_array.length>2){
document.write("In my zoo I've got ");
for (i=0; i<zoo_list_array.length; i++) {//loop for all species
if (zoo_list_array[i].length==1) animal=zoo_list[i];//1 individual
else animal=zoo_list[i]+"s";//more than 1 individual
if (i<zoo_list_array.length-2) document.write(zoo_list_array[i].length+" "+animal+", ");//from 1st species to 3rd last species in zoo
if (i==zoo_list_array.length-2) document.write(zoo_list_array[i].length+" "+animal+" and ");//second last species in zoo
if (i==zoo_list_array.length-1) document.write(zoo_list_array[i].length+" "+animal+".");//last species in zoo
}
document.write("<br><br>");
}//end of listing the species and their number of individuals
//listing the names of the individuals (per species)
for (j=0; j<zoo_list_array.length; j++) {
if (zoo_list_array[j].length==1){
document.write("The name of my " + zoo_list[j] + " is " + zoo_list_array[j]+".<br>");
}
if (zoo_list_array[j].length==2){
document.write("The names of my " + zoo_list[j] + "s are ");
document.write(zoo_list_array[j][0]+" and "+ zoo_list_array[j][1]+".<br>");
}
if (zoo_list_array[j].length>2){
arraylength=zoo_list_array[j].length-1;
document.write("The names of my " + zoo_list[j] + "s are ");
document.write(zoo_list_array[j][0]+", ");
for (i=1; i<arraylength-1; i++) {
document.write(zoo_list_array[j][i]+", ");
}
document.write(zoo_list_array[j][arraylength-1]+" and ");
document.write(zoo_list_array[j][arraylength]+".<br>");
}
}
document.write("<br>");
//-->
</script>
This code will write:
In my zoo I've got 1 giraffe, 2 lions, 1 tiger and 5 hippos.
The name of my giraffe is Long Neck.
The names of my lions are Clarence and Elsa.
The name of my tiger is Shir Khan.
The names of my hippos are Hippomom, Hippodad, Hippogirl, Hippoboy and Hippoaunt.
Sorry; I used different names (I wrote the stuff yesterday not having read your recent post). Cheers - Pit
omelette
11-17-2003, 09:24 PM
Hey, thanks, Pittiman! It's okay about the different names, I appreciate your time & effort :D.
I thought there may be a way to extract the variable's name. I'll use another array like you then ;). :cool:
omelette
11-24-2003, 01:24 AM
Hey Pittimann,
In case it helps you too, AdamBrill wrote to me with this suggestion & it helped me heaps:
<script type="text/javascript">
zoo_list = new Array();
zoo_list["hippos"] = new Array("Hippogirl", "Hippee");
zoo_list["lions"] = new Array("Lyan", "Grrr");
for(value in zoo_list){
document.write("My " + value + " are " + zoo_list[value] + "<br>");
}
</script>
Cheers! :)