Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

why my code doesn't stop in the while loop(in do while)when I write done,it will go,and go

Here my code

do{ guess=prompt("I am thinking the number of 1 to 10,What is its?"); Count=guessCount+1;

if(guess.toLowerCase()=="done"){
    checkG=true;
}else if(guess==randomNumber){
    checkG=true;
}else if(guess!=randomNumber){
    var G;

    while(G!=randomNumber){
        G=prompt(getRandomWord(10));
        Count=Count+1;
        if(G=="done"){checkG=true;}else if(G==randomNumber){checkG=true;}
        }
    }

}

while(!checkG)

3 Answers

In the inner loop your exit condition is while(G!=randomNumber). No matter what else you do, so long as G is not equal to randomNumber, you will be stuck inside the inner loop and never get to the end of your do...while loop! The code never gets as far as the line while(!checkG) unless you break out of the inner loop first.

thank you very much Sara how to break out the inner loop?

To be honest I don't understand why you need the second loop at all! Most of the code seems to be the same, so why can't you just use one loop?

If you don't want to change the code that much: try changing the while statement to say while(G!=randomNumber && !checkG)

Thank you very much now it work!