₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,326,969 members, 8,428,850 topics. Date: Thursday, 18 June 2026 at 06:06 AM

Toggle theme

JS Script Not Working(for_in Loop) - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingJS Script Not Working(for_in Loop) (1824 Views)

1 Reply (Go Down)

JS Script Not Working(for_in Loop) by melodyafsana(op): 9:17am On Apr 11, 2016
What's the problem with this js script? My browser doesn't working by it or going crashed or showing page unresponsive.
please check it & let me inform. Thanks in advance!
<script language="JavaScript">
var Players_Name=new Array()
Players_Name[0]= "L Messi"
Players_Name[1]= "Neymar"
Players_Name[2]= "C Ronaldo"
Players_Name[3]= "Suarez"
Players_Name[4]= "Higuean"
Players_Name[5]= "Kaka"

for(var i in Players_Name);
{
document.write{"The Players are :"+Players_Name[i]+"<br>"};
}




What is Crowdfunding? How to raise funds by crowdfunding? Which is the best crowdfunding site or platform to trust? The only answer is MyFundNow. 100% Campaign success with full fundraising!
Re: JS Script Not Working(for_in Loop) by Nobody: 9:45am On Apr 11, 2016
For loop carry 3 argument bro for (i=0 ,i<4 , ++i) ;
Re: JS Script Not Working(for_in Loop) by Nobody:
For loop carrys 3 arguments bro
for (i=0 ;i<4 ;++i) ;
Re: JS Script Not Working(for_in Loop) by Adesege(m): 10:09am On Apr 11, 2016
You are experiencing an endless loop and it can be frustrating seriously.

For loop takes three arguments; initialization, "limit" and increment.

You should do this instead

<script language="JavaScript">
var Players_Name=new Array()
Players_Name[0]= "L Messi"
Players_Name[1]= "Neymar"
Players_Name[2]= "C Ronaldo"
Players_Name[3]= "Suarez"
Players_Name[4]= "Higuean"
Players_Name[5]= "Kaka",
I;

for(i=0; i<Players_Name.length; i++)
{
document.write{"The Players are :"+Players_Name[i]+"<br>"};
}

Regards.
Re: JS Script Not Working(for_in Loop) by Craigston:
melodyafsana:
What's the problem with this js script? My browser doesn't working by it or going crashed or showing page unresponsive.
please check it & let me inform. Thanks in advance!
<script language="JavaScript">
var Players_Name=new Array()
Players_Name[0]= "L Messi"
Players_Name[1]= "Neymar"
Players_Name[2]= "C Ronaldo"
Players_Name[3]= "Suarez"
Players_Name[4]= "Higuean"
Players_Name[5]= "Kaka"

for(var i in Players_Name);
{
document.write{"The Players are :"+Players_Name[i]+"<br>"};
}
'i' is not an array index so [color=#118597]Players_Name[/color][i] is undefined.

Edit:

I got it wrong there.
i is actually an array index.
This works:

<script language="JavaScript">
var Players_Name=new Array()
Players_Name[0]= "L Messi"
Players_Name[1]= "Neymar"
Players_Name[2]= "C Ronaldo"
Players_Name[3]= "Suarez"
Players_Name[4]= "Higuean"
Players_Name[5]= "Kaka"

for(i in Players_Name)
{
document.write("The Players are: " + Players_Name[i] + "<br />" );
}
Re: JS Script Not Working(for_in Loop) by Nobody: 11:24am On Apr 11, 2016
Craigston:
'i' is not an array index so [color=#118597]Players_Name[/color][i] is undefined.
i need a value
I = 1
Re: JS Script Not Working(for_in Loop) by larisoft:
<script language="JavaScript">
var Players_Name=new Array()
Players_Name[0]= "L Messi"
Players_Name[1]= "Neymar"
Players_Name[2]= "C Ronaldo"
Players_Name[3]= "Suarez"
Players_Name[4]= "Higuean"
Players_Name[5]= "Kaka",
I;

for(var i in Players_Name);
{
document.write{"The Players are :"+Players_Name[i]+"<br>"};
}
you are using braces instead of brackets here:
document.write { "The Players are : " + Players_Name[i] + "<br>" };

change it to document.write("...."wink ;

also remove the ';' in front of your 'for' statement here

for(var i in Players_Name) ';'

it should be

for(var i in Players_Name) {

}

keep coding, bro.
Re: JS Script Not Working(for_in Loop) by helenfeona: 12:21pm On Apr 11, 2016
larisoft:
you are using braces instead of brackets here:
document.write { "The Players are : " + Players_Name[i] + "<br>" };

change it to document.write("...."wink ;

also remove the ';' in front of your 'for' statement here

for(var i in Players_Name) ';'

it should be

for(var i in Players_Name) {

}

keep coding, bro.
Ooops....it's embarrasing & my fault because I used {} it twice!
Thanks for suggesting smiley
Re: JS Script Not Working(for_in Loop) by helenfeona: 12:28pm On Apr 11, 2016
Adesege:
You are experiencing an endless loop and it can be frustrating seriously.

For loop takes three arguments; initialization, "limit" and increment.

You should do this instead

<script language="JavaScript">
var Players_Name=new Array()
Players_Name[0]= "L Messi"
Players_Name[1]= "Neymar"
Players_Name[2]= "C Ronaldo"
Players_Name[3]= "Suarez"
Players_Name[4]= "Higuean"
Players_Name[5]= "Kaka",
I;

for(i=0; i<Players_Name.length; i++)
{
document.write{"The Players are :"+Players_Name[i]+"<br>"};
}

Regards.
for(i=0; i<Players_Name.length; i++)
would you please explain why you didn't use var after for(?
Re: JS Script Not Working(for_in Loop) by Craigston: 12:40pm On Apr 11, 2016
proxy20:
i need a value
I = 1
Is that a different behavior in JS? In each nth iteration, i is a variable equivalent to Players_Name[n]. So in every iteration, the program outputs Players_Name[Players_Name[n]] instead of Players_Name[n].
Re: JS Script Not Working(for_in Loop) by Nobody: 12:54pm On Apr 11, 2016
Craigston:
Is that a different behavior in JS? In each nth iteration, i is a variable equivalent to Players_Name[n]. So in every iteration, the program outputs Players_Name[Players_Name[n]] instead of Players_Name[n].
yes .
Re: JS Script Not Working(for_in Loop) by asalimpo(m): 3:35pm On Apr 11, 2016
i dont do javascript but
isnt your logic toxic?

for(var i in Players_name)
document.write_Line(" "+i);

players_names is an array of Strings,
so 'i' is a string.
the type 'i' will be string . casting it , and trying to use it as an array indexer will cause casting errors
so chnge the line
"+Players_Name[i])...
it's logically wrong.

Use either the explicit for loop (<int>,<condition>,<int>wink
or the for-in loop (<type> in <collection)

Correct me house if i'm wrong..
See why languages with strong typing are recommended? They make certain kinds of errors impossible to commit.
Re: JS Script Not Working(for_in Loop) by asalimpo(m): 3:37pm On Apr 11, 2016
#this site sef

Seun, this is a programmer section, close-bracket and semi colon is symbolic to programming and entered a lot here, abeg, do something about these disgusting smileys .

It's getting too irritating...

Thank you.
Re: JS Script Not Working(for_in Loop) by larisoft: 3:43pm On Apr 11, 2016
asalimpo:
i dont do javascript but
isnt your logic toxic?

for(var i in Players_name)
document.write_Line(" "+i);

players_names is an array of Strings,
so 'i' is a string.
the type 'i' will be string . casting it , and trying to use it as an array indexer will cause casting errors
so chnge the line
"+Players_Name[i])...
it's logically wrong.

Use either the explicit for loop (<int>,<condition>,<int>wink
or the for-in loop (<type> in <collection)

Correct me house if i'm wrong..
See why languages with strong typing are recommended? They make certain kinds of errors impossible to commit.
I see what you mean, bro...but it just doesnt work that way in javascript. IF you run that code, you will see that I is actually just an index...not an array element.
Re: JS Script Not Working(for_in Loop) by Nobody: 4:21pm On Apr 11, 2016
Here is the corrected code:

<script language="JavaScript">
var Players_Name=new Array()
Players_Name[0]= 'L Messi';
Players_Name[1]= 'Neymar';
Players_Name[2]= 'C Ronaldo';
Players_Name[3]= 'Suarez';
Players_Name[4]= 'Higuean';
Players_Name[5]= 'Kaka';

for(i in Players_Name)
{
document.write("The Players are :"+Players_Name[i]+"<br>"wink;
}
</script>
Expected output:
The Players are :L Messi
The Players are :Neymar
The Players are :C Ronaldo
The Players are :Suarez
The Players are :Higuean
The Players are :Kaka
Re: JS Script Not Working(for_in Loop) by Adesege(m): 7:36pm On Apr 11, 2016
helenfeona:
for(i=0; i<Players_Name.length; i++)
would you please explain why you didn't use var after for(?
Hello, you will notice that at the tail end of your var declaration, there is an I before the semi-colon. That's where I declared it.

You can also declare it in the for loop.

Either way is good but since you have a variable declaration first, it will be best to declare it along side.

Hope you understand the logic now.

Regards
Re: JS Script Not Working(for_in Loop) by helenfeona: 6:46pm On Apr 14, 2016
okay, this problem has become solved & here is my output -
Thank you all guys!




What is Crowdfunding? How to raise money by crowdfunding? Which is the best crowdfunding site or platform to trust? The only answer is MyFundNow. 100% Campaign success with full fundraising!

Re: JS Script Not Working(for_in Loop) by Nobody: 9:15pm On Apr 14, 2016
Is alright, i have already solved it since now. Let us see your corrected code maybe we can learn from it.
Re: JS Script Not Working(for_in Loop) by helenfeona:
dhtml18:
Is alright, i have already solved it since now. Let us see your corrected code maybe we can learn from it.
yes, sure. I'm providing a screenshot of my coding here, my friend. & also if anyone think any error still exist on there please let me know.




What is Crowdfunding? How to raise funds by crowdfunding? Which is the best crowdfunding site or platform to trust? The only answer is MyFundNow. Start crowdfunding with 100% Campaign success & full fundraising!

Re: JS Script Not Working(for_in Loop) by freshPikin: 7:45pm On Apr 16, 2016
var Players_Name = [];
Only correction left.
Re: JS Script Not Working(for_in Loop) by Nobody: 4:09pm On Apr 17, 2016
freshPikin:
var Players_Name = [];
Only correction left.
Not necessarily bro, new Array() does the same thing.
1 Reply

Google Rewards Teenager $36,000 For Finding Security Loop In Their SystemHelp With Python Program (simple While Loop)Programming Challenge: Convert String to Json with a Loop234

Http Request/response TestingLet’s Build a Todo-List Web Application With ES6 Vanilla JavascriptThis is the best email marketing software in 2022