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 trialJesse Fister
11,968 PointsgetRandomNumber is not defined
my code is below. It is saying get RandomNumber is not defined: my code is identical to Dave's. Thanks!
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>");
Justin Swatloski
5,769 PointsYour function is called "randomNumber", but you're assigning "getRandomNumber" to the random number variable. Try
randomNumber = RandomNumber(upper);
See if that helps.
Marcus Parsons
15,719 PointsHey Jesse Fister.
I edited your code so that it would render better here on the forums. You can refer to the Markdown Cheatsheet for how I did that and/or the image at the bottom of this post.
David Langhorne and Justin Swatloski are correct that you are trying to reference a function that doesn't exist. Your function is named randomNumber
(the exact same as your variable). It would make sense to change your function to be getRandomNumber
so that way your code will execute and be more readable.
4 Answers
Ben Singer
8,621 PointsThe function is incorrectly named in the Workspace's app.js vs the function in the video. Slightly annoying.
Dave McFarland
Treehouse TeacherBlahhh!!! Sorry about that everyone. I've updated the workspace so that it matches the video. Sorry I didn't see this one sooner.
Hemant Kumar
870 Pointsfunction getRandomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; }
Jeremy Lindstrom
6,646 PointsAs Ben pointed out, the function in the workspace is incorrectly named, it should be function getRandomNumber(upper) and not function randomNumber(upper) they should be able to fix this quickly.
I also forwarded to support to fix the workspace.
Wes Laing
3,497 Pointsi think it should be != but I've never done javascript
Marcus Parsons
15,719 PointsYou should check out the comments first, Wes. But, no, !== is a valid comparison operator. It is the strict version of !=.
David Langhorne
7,681 PointsDavid Langhorne
7,681 PointsIn your example, the function is named randomNumber(), but you are trying to call a function named getRandomNumber(). I think randomNumber() was the name of the function in the previous video, but Dave changed to getRandomNumber() in this example - probably because he uses randomNumber as a variable name.