Reputation: 350
In my example count = 2. The first one works fine, but why does the second one not work? i equals 0 for all loops, so it become an infinite loop and crashes my browser tab.
I am not new to javascript but didn't encounter that problem before. The only explanation I can think of, is that in for loop it is not possible to increase the counting value by more than 1. But it does not make sense to me.
for (i = 0; i < count * 3; i++){
console.log(i);
};
Result : 0,1,2,3,4,5
for (i = 0; i < count * 3; i+3){
console.log(i);
};
Result: 0,0,0,0,0,0,0.....
Upvotes: 0
Views: 1003
Reputation: 1074198
Your increment is
i+3
...which doesn't do anything to write back to i
. To write back, use i+=3
:
for (i = 0; i < count * 3; i+=3){
The +=
operator adds the right-hand side to the left-hand side and stores the result in the left-hand side. The +
operator just does the addition, without storing the result.
Upvotes: 6