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

Qasim Hafeez
Qasim Hafeez
12,731 Points

Why doesn't while(true) work?

If while(!correctGuess) works because it evaluates to 'true', why doesn't while(true) work?

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'm thinking of a number between 1 and 10.  What is it?");
  guessCount+=1;
  if(parseInt(guess)===randomNumber){
    correctGuess=true;
      document.write("You got it!  The number was "+randomNumber+"! It took you "+guessCount+" tries!");    
  }
} while (!correctGuess);

When I tried while(true) it crashed the browser.

Konrad Pilch
Konrad Pilch
2,435 Points

We need the whole code.

4 Answers

Steven Parker
Steven Parker
231,275 Points

Replacing the last line with while(true); would work, but also cause the loop to repeat whether or not you guess the correct number.

So without any additional changes to the program, this makes the loop go on forever, it never stops asking for input, and the results of the document.write never get a chance to be rendered onto the page.

Is that what you meant by "crashed the browser"?

Now you could make the program work the same as before if you also add a break; statement right after the document.write. This would create another way to end the loop.

But testing the condition in the while (as you had originally) is better programming practice than relying on break.

Qasim Hafeez
Qasim Hafeez
12,731 Points

I think I get it now. What about while( guess !== randomNumber)? That's how I originally tried it before watching the solution and everything worked fine. Do you see any potential issues with that?

**I just remembered that my original code did not use a correctGuess variable at all.

Jack Gordon
Jack Gordon
9,232 Points

while(true) crashed your browser because that is an infinite loop. In this case, you need to tell the loop what it is defining to be true. While(true) is considered bad practice of code in most cases so watch out when using that!

Hope that makes sense.

Qasim Hafeez
Qasim Hafeez
12,731 Points

I just posted the whole code.

Qasim , I did it the same way you did, (parseInt(guess) !== rn). And it worked fine for me also. It just seems there are so many ways of doing things. I think they try to expose us to as many possibilities as is reasonable.

Jeremy Castanza
Jeremy Castanza
12,081 Points

Couldn't the while condition have been this instead...

while (correctGuess === false) 

Just curious as to why he didn't go this route.