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!

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

Eetu Hämäläinen
Eetu Hämäläinen
1,479 Points

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);

Eetu Hämäläinen
Eetu Hämäläinen
1,479 Points

Ok I had a bit misunderstanding. The number is between 1-10 so it can be any. So It is working normally!

1 Answer

Steven Parker
Steven Parker
228,039 Points

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".

Eetu Hämäläinen
Eetu Hämäläinen
1,479 Points

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 )

Steven Parker
Steven Parker
228,039 Points

For formatting posted code, see the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:
Or watch this video on code formatting.

As an example, and in answer to your question:

  else if (guessLeft < 1) {
    document.write("<p>You lose try again!</p>");
    break;                                         // add this to end the loop
  }