Friday, March 26, 2010

Loop not completing

I have a simple Loop in a function, that I'm using to remove all elements of the array:

for(var i:int = 0;?i %26lt; tempArray.length; i++)

{

tempArray.pop();

trace(''popping: '' + tempArray.length);

}?


The probelem i'm having is that the loop does not complete. It seems to only remove one element, somtimes it will remove two. Could an event that is triggered in a separate function occur that prevents this from executing to completion?

is there another method i could use to remove all the elements?

Loop not completing

use:


for(var i:int = tempArray.length-1;i%26gt;=0; i--)

{

tempArray.pop();

trace(''popping: '' + tempArray.length);

}?

Loop not completing

Haha, awesome, i'm honestly surprised that worked. i used:

for(var i:int = tempArray.length; i%26gt;0; i--)

{

tempArray.pop();

trace(''popping: '' + tempArray.length);

}

which is essentially the same thing. How come this works for decrementing and not for incrementing?

your first code snippet will fail because you are changing the array's length during each loop iteration and the end-condition of a for-loop is checked after each loop iteration.?the intial condition is only checked at the for-loop's start.

you could also hardcode your array's length:

var L:int=tempArray.length

for(var i:int =0;i%26lt;L; i++)

{

tempArray.pop();

trace(''popping: '' + tempArray.length);

}

OH! Look at that, thank you very much I don't know how I didn't see it before. Thank you again for your help.

you're welcome.

No comments:

Post a Comment