Jump to content

for loop


hisoka

Recommended Posts

the second statement in for loop tells how many times the loop will execute a piece of code :

 

var a = new Array("10","2");for(i=0 ; i<a.length ; i++){a=a+i;}

here in script the code is excuted endlessly although the second statement of the for loop is i<a.length . the length of the array is 2 so i<2 should loop one time . I do not understand why it loops endlessly???

 

 

Statement 2

Often statement 2 is used to evaluate the condition of the initial variable.

This is not always the case, JavaScript doesn't care. Statement 2 is also optional.

If statement 2 returns true, the loop will start over again, if it returns false, the loop will end.

 

 

what does this means :"If statement 2 returns true, the loop will start over again, if it returns false, the loop will end"??

 

 

Link to comment
Share on other sites

1. Don't use the Array() constructor. Instead declare an array with square brackets like this...

var a = ["10","2"];

2. If you have declared an array then use only legitimate operations on it. In your code above the statement a=a+1 makes no sense. See...

 

http://www.w3schools.com/jsref/jsref_obj_array.asp

 

3. If you have a for for-loop such as...

for(i=0 ; i<x ; i++){}

...then you would normally avoid changing the value of i or x inside that loop.

Link to comment
Share on other sites

The first problem has the loop body reassign the variable 'a' to a number. Given it's initial value of a.length as 2, and the initial value of 'i' as 0, 'a' will always be greater since the value of i is continuously added to it. So the loop never ends.

 

Thank you . However the loop looped endlessly not because of the because of this i<a.length but because of this

 

a=a+i; this is wrong and as a consequence of this error the loop looped endlessly

 

it must be like that

 

a=a[i+1] ;a=a[i+2] until 4 // this to change the position of the element in the array

 

a=a+1; until 1000000000000.... // this to add the first element of the array to the any number you want

 

 

a=a+i; it works too and is true

 

var a = ["10","2"]; this was an error : I do not use a debugger . I debug from my brain . So that it is sometime , difficult to notice such errors

 

thank you very much for the link and keep correcting me .

Edited by hisoka
Link to comment
Share on other sites

Your problem is that a is an array. You cannot add a number to an array

 

When you write a = a + i, what are you expecting it to do?

 

What's happening in your script is that it casts the array to a string "10,2" and then concatenates i, resulting in "10,20". On the second iteration, a.length is now the length of the string, which is 5. Now a = a + i results in the string "10,201" which has length 6. On the third iteration a becomes "10,2012", a string with length 7. And this continues on and on, because a.length keeps getting bigger and i never catches up with it.

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
×
×
  • Create New...