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

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Random Number Challenge Solution

Umair Khalil
Umair Khalil
5,285 Points

Why do I get this error?

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

That's my code , I copied it word for word from the video but for me I keep getting an this error:

Uncaught SyntaxError: Unexpected token ;

What is wrong with my program, aren't I suppose to get the semicolon at the end.

3 Answers

dan lematy
dan lematy
4,157 Points

you are missing a curly brace, the correct code is

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

Chris Buczkowski
Chris Buczkowski
3,939 Points

It looks like the problem is one of the parentheses isn't closed. You've got:

(lower, upper) - good!

(Math.random() <---notice the opening parenthesis here before the 'M' in Math. This doesn't have a closing parenthesis.

(upper - lower + 1) - good!

not sure if the formatting got lost when you posted it, but following the formatting practices in the course, it should look something like this:

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

also, assuming you're using Workspaces, if you position the cursor next to one of your braces (curly or parenthesis), the brace/parenthesis will appear red if it's not closed. if it is closed, it will show highlighted in yellow, and the other associated brace or parenthesis that opens or closes it will also be highlighted.

(code <--cursor next to the parenthesis would cause it to appear in red (code) <--cursor next to either parenthesis would cause both parentheses to highlight yellow

I've found this, and judicious use of the JavaScript console extremely helpful in identifying where I've missed something in my code.

Tommy Izen
Tommy Izen
5,746 Points

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

Your code above is missing a closing parenthesis after "lower" argument:

The code below should work fine.

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