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

Sergey Scherbo
Sergey Scherbo
5,784 Points

I really dont understand why

Why we can't go like this:

do { guessNum = prompt("Please, guess the number"); } while (parseInt(guessNum) === randomNum);

I really don't understand why it doesn't work.

3 Answers

Juan Martin
Juan Martin
14,335 Points

Hey there!

The code looks good, though there's a little logical mistake at the end, the loop should keep going until the number is guessed so:

while (parseInt(guessNum) !== randomNum);

Hope this helps!

Hi Sergey

I suggest that you check your while condition again. As it is right know, it will run as long as you guess the correct number. If it is wrong, then the loop breaks.

Egarat

Can Someone tell me what's wrong with this code block.

var randomNumber = getRandomNumber(2);
var guess;
var guessCount = 0;

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

do{
  guess = prompt("Please guess a number between 1 to 2.");
  guessCount += 1;
}while(parseInt(guess) !== randomNumber);

document.write("<p>" + "You took " + guessCount + " attempts to guess the right number." + "</p>");