/    Sign up×
Community /Pin to ProfileBookmark

my variable is not recognised as one.

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.

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@skriptorNov 12.2003 — Hi,

your for-statement is wrong. Try something like this
[code=php]for (i=0; i<hippo.length; i++) { }[/code]
Good Luck, skriptor
Copy linkTweet thisAlerts:
@ScriptageNov 12.2003 — 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);

...
Copy linkTweet thisAlerts:
@omeletteauthorNov 13.2003 — 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! ?
Copy linkTweet thisAlerts:
@omeletteauthorNov 13.2003 — 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".
Copy linkTweet thisAlerts:
@PittimannNov 13.2003 — Hi omelette!

Do you want this to be put out:

The names of my hippos are Hippogirl,Hippee ???
Copy linkTweet thisAlerts:
@omeletteauthorNov 14.2003 — 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>");

}
Copy linkTweet thisAlerts:
@PittimannNov 14.2003 — Hi omelette!

Of course this is possible. Here is some code which also checks singular and plural:

[code=php]
<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>
[/code]

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
Copy linkTweet thisAlerts:
@omeletteauthorNov 18.2003 — Hey, thanks, Pittiman! It's okay about the different names, I appreciate your time & effort ?.

I thought there may be a way to extract the variable's name. I'll use another array like you then ?. ?
Copy linkTweet thisAlerts:
@omeletteauthorNov 24.2003 — Hey Pittimann,

In case it helps you too, AdamBrill wrote to me with this suggestion & it helped me heaps:
[CODE]
<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>[/CODE]


Cheers! ?
×

Success!

Help @omelette spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...