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.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

Erik L
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik L
Full Stack JavaScript Techdegree Graduate 19,469 Points

Why isn't my code working?

my code is not working

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('I am thinking of a number between 1 and 10. What is it?');
  guessCount += 1;
  if (parseInt(guess) === randomNumber) {
    correctGuess = true;
  }
} while ( ! correctGuess )

document.write('<h1>You guessed the number!</h1>');
document.write('It took you ' + guessCount + ' tries to guess the number ' + randomNumber);

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Your code is just fine! So the question becomes why it works for me and not for you. Here are some things to check:

  • make sure you saved your JavaScript file
  • make sure your link to your JavaScript file is correct
  • if the above are correct, you might need to clear your browser's cache. It's possible that your browser is loading in an older version of your code instead of this version.

Hope this helps! :sparkles:

Jennifer is right.....upvoted.

You never added the curly braces for the while loop.

do {
  guess = prompt('I am thinking of a number between 1 and 10. What is it?');
  guessCount += 1;
  if (parseInt(guess) === randomNumber) {
    correctGuess = true;
  }
} while ( ! correctGuess ) {
   document.write('<h1>You guessed the number!</h1>');
   document.write('It took you ' + guessCount + ' tries to guess the number ' + randomNumber);
}
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Elijah Quesada The student who posted the question has a functional do while loop. Your code will produce an infinite loop which hangs the browser as it contains no code to set the correctGuess to true. Take a look at the code in the question again :smiley: Thanks for helping out in the Community! :sparkles:

edited for additional comment

Sorry, maybe I misunderstood what you meant. I thought, originally, that you meant to remove the do part and just have the while part. If that is not what you meant, then it would not be an infinite loop, but adding the curly braces will have no effect :smiley: