11/11 infinite loops


#1

I can't see what I have done wrong,

for(var i=1; i < 4; i++){
console.log(i);
};

var whileNumber= true;
while(whileNumber){
console.log("Huzzah!");
whileNumber = false;
};
console.log("It's Tricky!");

var doNumber= 1;
do{
console.log("Gadzooks!");
}
while(doNumber);


#2

? This isn't 11/11 in Intro to While Loops in JS. Can you say the exact level?


#3

Hmm, its 11/11 of intro to 'while' loops in JS. Review level.

You have to write a for, while, and do loop.


#4

Ohhh thanks. I forgot that was a custom one.


#5

The problem is wit this line

var doNumber= 1;
do{
    console.log("Gadzooks!");
}while(doNumber);

the variable doNumber is set to be 1 and it will always be true that its value is 1 hence because you used it as your while condition the loop is infinite. this is analogous to saying

do{
    //something
}while(true);//this is an infinite loop

what you can do to stop the loop is to insert a break statement like below

var doNumber= 1;
do{
    console.log("Gadzooks!");
    break;
}while(doNumber);

#6

I think you misunderstood the meaning of a do/while loop. That is not how you are supposed to do it, well, I actually kinda tweaked your code a little to make it work! Look at it and look at how the do/while loop looks like.

for(var i=1; i < 4; i++){
console.log(i);
};

var whileNumber= true;

whileNumber = false;
do{
console.log("Gadzooks!");
}
while(whileNumber);

Hope it helped!:smiley:


#7

Thank you to both of you! The help is much appreciated.