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

Nicholas Evans
Nicholas Evans
7,911 Points

Why does my browser crash while running this program?

Everytime I try to run this loop, my browser always crashes. It seems like an infinite loop! Could someone help to see what I am doing wrong?

var randomNumber = getRandomNumber(5); var guess; var guessCount = 0; var correctGuess = false;

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

do { guess = prompt("Give me a number between 1 and 10"); guessCount += 1; if (parseInt(guess) === randomNumber) { correctGuess = true; } } while ( ! correctGuess ) document.write ("Congrats! You picked the correct number! Although it took you " + guessCount + " guesses");

John Erickson
seal-mask
.a{fill-rule:evenodd;}techdegree
John Erickson
Full Stack JavaScript Techdegree Student 3,916 Points

Hey Nicolas,

So this is my second time around with this video series; the first time I had a lot of confusion. Regardless, I looked at your code and it runs fine. I tested it twice now and I've experienced no issues, no infinite loop. I hope you were able to resolve the issue.

Thanks, John

3 Answers

Steven Parker
Steven Parker
231,269 Points

I noticed that this program asks for guesses between 1 and 10 yet it only picks numbers from 1 to 5.

But otherwise, it seems to work fine. No crashing here.

Charlie Sattgast
Charlie Sattgast
7,205 Points

I'm having the same issue. I've got exactly the same code as Dave but the program never returns a result where I have selected the correct number and eventually delivers a fatal error. I have examined every single character in the code and cannot find any variation from Dave's version.

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Do you always type the same number into the prompt box?

John Erickson
seal-mask
.a{fill-rule:evenodd;}techdegree
John Erickson
Full Stack JavaScript Techdegree Student 3,916 Points

Good afternoon Arikaturika,

The prompt is for the user to select numbers ranging from 0 to 10 in hopes of selecting the random number correctly. That being said you don't want to select the same number over and over again. I hope this helps.

Thanks, John