## Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today. # My code not working loop won't end

Why this is not ending the loop when the correct answer is given?

var randomNumber = getRandomNumber(10); var guess; var guessCount = 0; var correctGuess = false;

function getRandomNumber( upper ) { var num = Math.floor(Math.random() * upper) + 1; return num; }

do { guess = prompt('Write the number between 1-10.'); guessCount +=1; if (parseInt(guess) === randomNumber) { correctGuess = true; } } while ( ! correctGuess )

document.write('h1>Yeah thats right!</h1>'); document.write('It took you ' + guessCount + ' tries to guess the number ' + randomNumber); This code appears to be correct. The loop will continue until you guess the random number.

If you're inclined to experiment, one possible improvement would be to keep a count of the tries and only allow a certain number of attempts. Then you would have two endings, one for "win" and one for "lose". Hi,

Here is my code now. It keeps tracking how many wrong answer I have, but it won't end when 0 guesses are left. I'm not sure how to do it correctly with do while loop. Can I make different end to the code?

var randomNumber = getRandomNumber(10); var guess; var guessCount = 0; var correctGuess = false; var guessLeft = 10;

function getRandomNumber( upper ) { var num = Math.floor(Math.random() * upper) + 1; return num; }

do { guess = prompt('Write the number between 1-10. ' + 'You have ' + guessLeft + ' guesses left.'); guessCount +=1; guessLeft -=1; if (parseInt(guess) === randomNumber) { correctGuess = true; document.write('<h1>Yeah thats right!</h1>'); document.write('It took you ' + guessCount + ' tries to guess the number ' + randomNumber); } else if (guessLeft < 1) { document.write('<p>You lose try again!</p>'); } } while ( ! correctGuess ) For formatting posted code, see the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.

```  else if (guessLeft < 1) {