PDA

View Full Version : For i<array.length Not Doing Whole Array



pi3rc3
August 7th, 2009, 09:32 PM
this problem is blowing my mind i have a for loop that i want to go through my entire array but it seems to not be counting the last element in the arraay!

for(var i:int=0; i<gridControl.game.blockG.length; i++){
trace(i +" "+gridControl.game.blockG.length);
}

and the last line of code it spits out is "83 84" meaning that there are 84 items in the array but it only ran through to 83...i dont understand why its doing this.

Tidenburg
August 7th, 2009, 10:02 PM
Actually, it is going through the whole array. If you look at the beginning your first value is 0 (your first output will be 0 84). You don't normally include 0 when counting so it just looks like it isn't because you're not counting the extra index counted at the start. This is why you have "i < array.length" instead of <=.

rumblesushi
August 7th, 2009, 10:05 PM
It's checking all of them. "i" ends at 83 because it starts at zero.

And obviously it needs to start at zero because the first element of the array is [0].

blockG[83] is actually the last element in the array, because although it has 84 elements, it starts at zero.

pi3rc3
August 7th, 2009, 10:30 PM
wow do i feel silly. id like to apologzie for wasting everyones time.