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
Tomasz Hermanowicz
7,219 Pointsproblem with JavaScript "crypto" game.
what is wrong with my code, chrome die all the time.
var upper = 10000; var randomNumber = getRandomNumber; var guess; var attemps = 0;
function getRandomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; } while (guess !== randomNumber) { guess = getRandomNumber(upper); attemps += 1; }
document.write("Random number was " + randomNumber); document.write("it took the computer" + attemps);
1 Answer
akak
29,446 PointsYou didn't call the getRandomNumber function while declaring variable randomNumber. So at this point guess will be always not equal randomNumber since it's a function, not a number. And so endless loop forms :)
Change to
var randomNumber = getRandomNumber(upper);
and you should be good.
Tomasz Hermanowicz
7,219 PointsTomasz Hermanowicz
7,219 PointsTHX