Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,206,563 members, 7,996,088 topics. Date: Wednesday, 06 November 2024 at 09:41 PM

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

Nairaland Forum / Science/Technology / Programming / JS Script Not Working(for_in Loop) (1729 Views)

Google Rewards Teenager $36,000 For Finding Security Loop In Their System / Help With Python Program (simple While Loop) / Programming Challenge: Convert String to Json with a Loop (2) (3) (4)

(1) (Reply) (Go Down)

JS Script Not Working(for_in Loop) by melodyafsana(f): 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: 9:45am On Apr 11, 2016
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: 10:39am On Apr 11, 2016
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 Players_Name[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 />" );
}

1 Like

Re: JS Script Not Working(for_in Loop) by Nobody: 11:24am On Apr 11, 2016
Craigston:

'i' is not an array index so Players_Name[i] is undefined.
i need a value
I = 1
Re: JS Script Not Working(for_in Loop) by larisoft: 11:35am On Apr 11, 2016

<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

3 Likes

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

2 Likes

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: 5:50pm On Apr 16, 2016
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)

I Need Someone Who Will Teach Me Java And Sql Server 2008 / Help on C Programming / Java JDBC Resultset.next PROBLEM

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 37
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.