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 trialNicholas Evans
7,911 PointsWhy 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");
3 Answers
Steven Parker
231,275 PointsI 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
7,205 PointsI'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.
Dante Alston
988 PointsSame issue...
John Erickson
Full Stack JavaScript Techdegree Student 3,916 PointsHey Charlie,
Are you able to post your code?
Thanks, John
Arikaturika Tumojenko
8,897 PointsDo you always type the same number into the prompt box?
John Erickson
Full Stack JavaScript Techdegree Student 3,916 PointsGood 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
John Erickson
Full Stack JavaScript Techdegree Student 3,916 PointsJohn Erickson
Full Stack JavaScript Techdegree Student 3,916 PointsHey 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