Jump to content

JS Loop


shreyaskudav
 Share

Recommended Posts

<html><body><script type="text/javascript">var i=0;for (i=0;i<=5;i++){document.write("The number is " + i);document.write("<br />");}</script></body></html>

This script loops from 0,1,2,3,4,5,6 etc...etc...etc...But what if i want to increment it, 2 or more times...Like: 2,4,6,8,10or 5,10,15,20 ??

Edited by shreyaskudav
Link to comment
Share on other sites

there are a couple options. one way is to do something like this:

<html><body><script type="text/javascript">var i = 1, end = 100, modulus = 5;for (i; i <= end; i++){  if(i%modulus == 0){	console.log('The number is ' + i); 	//would log 5, 10, 15, 20, etc all the way to 100};</script></body></html>

if you wanted to do the same thing for even numbers, you would only have to change the modulus variable to 2.edit:http://www.w3schools.com/js/js_operators.asp

Edited by thescientist
Link to comment
Share on other sites

Yet another way is to print a multiple of i. That's the good thing about programming, there are many ways to do things:

for (i = 1; i <= 5; i++){	document.write('The number is ' + (i * 2) );	document.write("<br />");}

Link to comment
Share on other sites

... That's the good thing about programming, there are many ways to do things:
may i join in too ! :) (i ilke JSG's way best, most efficient for stated purpose)here's my inefficient way.
<html><body><script type="text/javascript">var var j=1; var skip=4;for (i=0;i<=(60/skip);i++){j+=skip;document.write("The number is " + j);document.write("<br />");}</script></body></html>

ahh... i changed it to j=0 and now realise why it's good to start from zero ! :)

Link to comment
Share on other sites

hey, thanks, its quiet easy and very nice to sfind so many ways out...:)But......

<html><body><script type="text/javascript">var f=1;for (f=1;f<9;f++);{document.write("Number" + f);document.write("<br />");}</script></body></html>

Whats the problem in this???And one thing more...

(i = 1; i <= 5; i++)

/

(i=1;i<=5;i++)

Which one is correct?

Edited by shreyaskudav
Link to comment
Share on other sites

may i join in too ! :) (i ilke JSG's way best, most efficient for stated purpose)here's my inefficient way.
<html><body><script type="text/javascript">var var j=1; var skip=4;for (i=0;i<=(60/skip);i++){j+=skip;document.write("The number is " + j);document.write("<br />");}</script></body></html>

ahh... i changed it to j=0 and now realise why it's good to start from zero ! :)

well, you certainly got the ineffecient part right. certainly made me scratch my head a couple of times... xD(although I would watch for script trace errors when doing this: var var j = 1)
hey, thanks, its quiet easy and very nice to sfind so many ways out...:PBut......
<html><body><script type="text/javascript">var f=1;for (f=1;f<9;f++);{document.write("Number" + f);document.write("<br />");}</script></body></html>

Whats the problem in this???And one thing more...

(i = 1; i <= 5; i++)

/

(i=1;i<=5;i++)

Which one is correct?

1) you shouldn't have a semicolon here:
for (f=1;f<9;f++);

2) neither is more correct. one (IMO) is just easier to read.

Edited by thescientist
Link to comment
Share on other sites

well, you certainly got the ineffecient part right. certainly made me scratch my head a couple of times... :)
would that indicate that i can handle more complex control structures ? :) << runs & hides >>
(although I would watch for script trace errors when doing this: var var j = 1)
whoops, i had var i=0 as well, but only half deleted it before posting.
Link to comment
Share on other sites

would that indicate that i can handle more complex control structures ? :) << runs & hides >>
errrr..... Edited by thescientist
Link to comment
Share on other sites

errrr.....
truth be told, i scratched my head several times in *creating* it as it didn't have the desired effect when first made ! :)(talk about complicating things !!)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...