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!

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

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Jesse Fister
Jesse Fister
11,968 Points

getRandomNumber 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>");

In 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.

Your function is called "randomNumber", but you're assigning "getRandomNumber" to the random number variable. Try

randomNumber = RandomNumber(upper);

See if that helps.

Hey 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

The function is incorrectly named in the Workspace's app.js vs the function in the video. Slightly annoying.

Dave McFarland
Dave McFarland
Treehouse Teacher

Blahhh!!! Sorry about that everyone. I've updated the workspace so that it matches the video. Sorry I didn't see this one sooner.

function getRandomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; }

As 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.

i think it should be != but I've never done javascript

You should check out the comments first, Wes. But, no, !== is a valid comparison operator. It is the strict version of !=.