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

Somehow I made an infinite loop..that's not stopping!

Hello I followed Dave exactly still my codes have created an infinite loop that's not stopping. Please guide me where I did mistake. Here it is:

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('<p>I am thinking of a random number between 1 and 10. What is it? </p>');
 guessCount += 1; 
 if (parseInt(guess)===randomNumber){
    correctGuess=true;
 }
} while( !correctGuess )
  document.write('<h1>Congrats. You guessed the number. </h1>');
 document.write('It took you ' + guessCount+ ' tries to guess the  ' + randomNumber);
Thomas Nilsen
Thomas Nilsen
14,957 Points

I tested your code, and it runs fine.

4 Answers

Aaron Loften
Aaron Loften
12,864 Points

Dude, this is killing me. I cant....put it down. It is weird. It appears that this is something wrong with the if validation.

Im at the point where I promised myself ID stop looking at this.

Could be one of a thousand things. All I know, if you replace this with a static truth, there is no inifinite loop.

Consider retyping out the entire code. use a regular keyboard, a regular text editor, and then run it. DO NOT USE MSWORD AND DO NOT COPY AND PASTE. Its usually not bad to copy/paste, but when debugging such a.....dumb....annoying issue, I recommend you do your best to not cause an unseen issue such as an ms character replacement or a "pretty paste". To avoid a pretty paste, use right-click -> paste as plain text. Avoiding pretty paste will not prevent ms character replacement, which is when commas and quotes and even periods(I think periods....) are replaced by almost identical-looking characters that cause your code to fail.

Okay...Im done obsessing. Good luck. It does look almost identical to the teacher's.
Tsenko Aleksiev
Tsenko Aleksiev
3,819 Points

Hi there! Your code works just fine. In my opinion you just can't guess the random number that's why your loop keeps it going :) I have tried your code in jsfiddle, but changed the getRandomNumber(3); and tried it out: it works fine, when I guess the number it prints out the message. Probably it would be ok if you add a break condition if the user want's to stop trying to guess the number, something like: if (parseInt(guess)===randomNumber){ correctGuess=true; } else if (guess === 'quit'){ document.write('Quiting allready? :('); break; } And that's it...I think :)

Thanks Aleksiev, Loften and Nilsen for your responses.

I am checking this out.

David Foord
David Foord
3,545 Points

If you choose the same number every time, it will be wrong every time. The number has been chosen, so if your first guess was wrong, it will still be wrong the second time and the third and so on infinitum. Change your number each time and you will get to the correct number.

I think I made the same mistake as you and just kept guessing the same number over and over (sent me mad too). The browser only chooses a new number when it is refreshed.