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 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,470 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: