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 trialRoudy Antenor
4,124 PointsEndless loop! - yet everything is verbatim
'''javascript var randomNumber = getRandomNumber(3); var guess; var guessCount = 0; var correctGuess = false;
function getRandomNumber ( upper ) { var num = Math.floor (Math.random() * upper) +1; return num; }
do { guess = prompt("guess a number between 1 and 3"); guessCount += 1; if (parseInt (guess) === randomNumber) { correctGuess = true; } }while (! correctGuess)
document.write ("your number of guesses " +guessCount ) '''
Hello everyone - above is my code - and i do not understand why i continue to get an endless loop - if i change the random generator to go up to 1 only - the program works, BUT anything higher than that (say 3 ) and here comes BROWSER CRASH CITY!!
Please help
Chris Slack
28,500 PointsThis code seems to work fine as far as I can see. Try it here:
https://jsfiddle.net/jzz54mnp/
I added the actual number you are trying to guess into the prompt so that I didn't really have to guess, and it appears to keep going back to the prompt if I get the answer wrong but prints out the number of guesses if I get it right. This seems to be the right behavior.
For a more compact version of the code see here :-)
https://jsfiddle.net/p5f8bu1e/
Chris
2 Answers
Steven Parker
231,269 PointsYour code looks good.
As Chris pointed out, the current code seems designed to loop until a correct guess is given. This might seem "infinite" if you don't guess the correct number, which will get harder as the range is expanded.
One possible enhancement to the program that might help (and make it more fun) is to print out a hint after each try saying if the guess was too high or too low.
Roudy Antenor
4,124 PointsThan you all for your review of my code -- I did see what i was missing/ doing wrong -- BIG THANKS !!
Timothy Suellentrop
17,120 PointsTimothy Suellentrop
17,120 PointsIf you refer to the Markdown Cheatsheet under where you typed your question in, you can see how to use 3 backticks (`) to make your code easier for us to read and help you out.