I have this bizzare behavior which I’ve never seen and don’t understand. I’m going through an array using arrayname.length as the control for a while loop. Which normally works as expected but this particular case creates an endless loop when the length of the array is not reduced beyond the first iteration.
while( data.length > 0){
finalCheck = finalCheck && data.pop();
}
Here, the initial length is 4 and finalCheck starts at true. The first couple array elements are false.
As expected on first iteration, finalCheck becomes false and data.length becomes 3 but then for every iteration afterwards data.length stays at 3… does anyone know why this would happen?
finalCheckbecomes false at some point, causingdata.pop()to not be executed. Then, the loop becomes infinite.data.pop()returns the removed element from the array. If it’s a value evaluating to false, no elements will be removed at the next iteration, causingdata.lengthto always be higher than zero.In the previously shown example,
finalCheckis alreadyfalsebefore the iteration, causingdata.pop()to never be executed. Even iffinalCheckis initialized attrue,finalCheckwill be set to zero, and be stuck again.To fix your code, you should AT LEAST use: