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, Part II

Ruben Lindskog
Ruben Lindskog
1,008 Points

"Uncaught SyntaxError: Unexpected token ." ?

function randomNumber ( upperNumber, lowerNumber ) { var Math.floor(Math.random() * (upperNumber - lowerNumber + 1)) + lowerNumber; if ( isNan(upperNumber) = true || isNaN(lowerNumber ) = true ) { throw new Error("You've something that's not a number, the functiion will only work with numbers" ); } return Math; } returns the error in the title and i can't find where it comes from

Ruben Lindskog
Ruben Lindskog
1,008 Points

oh yeah, the error is in the "var Math.floor(Math.random() * (upperNumber - lowerNumber + 1)) + lowerNumber; "

2 Answers

Steven Parker
Steven Parker
231,269 Points

It's a bit tough to read unformatted code, for future postings please use Markdown formatting. Instructions can be found in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or for more detailed instructions, you can watch this video on code formatting.

But I managed to spot a few issues in addition to the ones Emmanuel pointed out anyway:

  • "var" should be used to declare a variable, but here it is followed by a math expression
  • you wouldn't want to re-assign the word "Math", it refers to a library you need
  • the "isNaN" function must be spelled with two capital "N"s (you have "isNan" in one case)
  • you probably want to test the arguments before you try to perform math on them
Ruben Lindskog
Ruben Lindskog
1,008 Points

I'll remember to use markdown formatting in the future! Oh yeah, I originally used return instead of var before math so the transition must've messed me up. Should've tested the arguments after I changed to var too. Thank you for all the help!

Emmanuel C
Emmanuel C
10,636 Points

It looks like you didnt name the var thats going to hold that floored number.

Also in your if statement you compare statements with double "==" or triple equals "===". But since isNan() returns a boolean you dont need to compare it to true, so this would do ...

if (isNan(upperNumber) || isNaN(lowerNumber) ){}
//otherwise it would be
if (isNan(upperNumber) == true || isNaN(lowerNumber) === true )