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
Thanitsak Leuangsupornpong
7,490 Pointswhy 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
Sara Chicazul
7,475 PointsIn 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.
Thanitsak Leuangsupornpong
7,490 Pointsthank you very much Sara how to break out the inner loop?
Sara Chicazul
7,475 PointsTo 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)
Thanitsak Leuangsupornpong
7,490 PointsThank you very much now it work!