8. When to 'while' and when to 'for'


#1

for (var i = 10; i < 20; i++) {
console.log(i);
}

var speed = 10;

while(speed) {
console.log("Speed Up!");
speed=10;
}
loop();

I keep getting an infinte loop and my browser ends up crashing. What could be wrong? I've already determined that the for loop is fine. So the problem is with the while loop. I can't seem to figure it out though...


#2

speed's always going to = 10 so the loop will just keep running over and over.

I got it to work by changing it to this

var speed = true;

while(speed) {
console.log("Speed Up!");
speed = false;
}

#3

Thanks! It worked! :slightly_smiling: