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 trialMICHAEL COOK
7,248 PointsgetRandomNumber is not defined
I tried to run the following code in my browser while going along with the video and the page showed up as blank. So when I inspected the page and went to console i got this message : app.js:2 Uncaught ReferenceError: getRandomNumber is not defined at app.js:2
this is the code:
var upper = 10000; var randomNumber = getRandomNumber( upper ); var guess; var attempts = 0;
function randomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; }
while ( guess !== randomNumber ) { guess = getRandomNumber( upper ); attempts += 1; } document.write("<p>The random number was: " + randomNumber + "<p>"); document.write("<p>It took the computer " + attempts + "attempts to get it right.</p>")
I can't figure out where I messed up because it all looks accurate and like I followed along in the video...
2 Answers
Stephen Fowles-White
10,457 PointsHi Michael,
When you declare the variable:
var randomNumber = getRandomNumber( upper )
You are calling the function getRandomNumber with the variable upper in it. But the function on the above code is
function randomNumber(upper) not getRandomNumber( upper ). You just need to make these function names the same and it should work.
This code worked for me.
var upper = 10000; var randomNumber = getRandomNumber(upper); var guess; var attempts = 0;
function getRandomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; }
while ( guess !== randomNumber ) { guess = getRandomNumber( upper ); attempts += 1; }
document.write("<p>The random number was: " + randomNumber + "<p>");
document.write("<p>It took the computer " + attempts + "attempts to get it right.</p>")
I hope this helped
Stephen
MICHAEL COOK
7,248 PointsThank you for the help with that, i just don't understand why the original code i posted worked for him in the video, but not for me. Either way, again, I appreciate the help!
Stephen Fowles-White
10,457 PointsHappens to me too, glad it helped :)