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 trialLiam Greig
2,233 PointsOuff. What am I missing here?
I feel like I must be missing something obvious here. The code for this video makes sense to me but I can't get it to run without crashing my browser. What am I missing? Thanks!
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 am thinking of a number between 1 and 10. What is it?');
guessCount += 1;
if (parseInt(guess) === randomNumber){
correctGuess = true;
}
} while ( ! correctGuess )
document.write('You got it!');
5 Answers
Liam Greig
2,233 PointsJennifer Nordell , Steven Parker soooo... turns out I'm an idiot. I was guessing the same number over and over each time - totally gapped on the fact that the randomNumber is set once and doesn't change with each guess ?
Oops. Thanks for your help. Onwards!! ?
Steven Parker
231,275 PointsThis code looks good to me. But be aware that it will continue to demand input until you enter the correct number. Is that what you were calling "crashing"?
Jennifer Nordell
Treehouse TeacherHi there! To be honest, I don't think you're missing anything. Your code runs just fine in my browser.
This makes me a little suspicious about your browser. In the front-end courses workspaces is essentially just a convenient text editor, but the code is executed on your machine in your browser. There is the off chance that what is being loaded in is not the code listed here, but rather a cached version of your code, which may have contained some error that was causing an infinite loop. I'm curious what happens if you clear the browser cache and start the preview and refresh the page. If this doesn't work, what browser and version are you using?
Looking forward to your response!
Jennifer Nordell
Treehouse TeacherI am assuming you are starting from 1 and working all the way up to 10? That's what I do and eventually it will be correct.
Liam Greig
2,233 PointsYeah, something odd is going on. I've cleared my cache and tried running the same program in FF and still getting the same response - I'm prompted to guess a number but my 'guess' is always wrong. Eventually the browser becomes unresponsive and I have to quit and start again...
Steven Parker
231,275 PointsTry a different browser? I rarely have any trouble using Chrome.
Liam Greig
2,233 PointsI think I was just getting unlucky. When the program runs, it sets the random number between 1 and 10 - let's say it was set to 8. Then I'd just keep guessing 1 over and over so it was never the correct guess. I must have tried that a couple of dozen times without ever getting it right.
I'm not sure why the browser was becoming unresponsive either though. Maybe because the program was still trying to run the do ... while loop? A bit odd for sure...
Steven Parker
231,275 PointsThat would be guessing the same number each time, and yes, that would explain the program never ending (but not the unresponsiveness).
Steven Parker
231,275 PointsSteven Parker
231,275 PointsBut isn't the whole point that if you guess a different number each time you'll eventually get the right one? And I still don't get why the browser would become unresponsive after a while.