I'm trying to create a while loop with a continue statement. However it seems to be causing an infinite loop and I can't figure out why.
The code below to me seems like it should start with the var tasksToDo at 3 then decrement down to 0 skipping number 2 on the way.
var tasksToDo = 3
while (tasksToDo > 0) {
if (tasksToDo == 2) {
continue;
}
console.log('there are ' + tasksToDo + ' tasks');
tasksToDo--;
}
解决方案
conitnue, will go back to the while loop. and tasksToDo will never get decremented further than 2.
var tasksToDo = 3
while (tasksToDo > 0) {
if (tasksToDo == 2) {
tasksToDo--; // Should be here too.
continue;
}
console.log('there are ' + tasksToDo + ' tasks');
tasksToDo--;
}