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 Solution

UnCaught error: Both arguments must be numbers

Hi, in the random number challenge part II, i keep getting this error message in my console and can't figure out why. Any help?

function getRandomNumber( lower, upper ) {
  if ( isNaN(lower) || isNaN(upper) ) {
    throw new Error('Both arguments must be numbers');
  } 
return Math.floor(Math.random() * (upper - lower + 1)) + lower; 
}

console.log( getRandomNumber( 9, 24 ) );
console.log( getRandomNumber( 1, 100 ) );
console.log( getRandomNumber( 200, 'five hundred' ) );
console.log( getRandomNumber( 1000, 20000 ) );
console.log( getRandomNumber( 50, 100 ) );

3 Answers

Tom Gooding
Tom Gooding
16,735 Points

Hi Bill,

This is intentional to throw the error as Dave explains in the video.

You are getting this error because you are trying to pass the string of 'five hundred'. The second line of code checks whether the 'upper' and 'lower' are values using the 'isNaN' function. This will return 'true' as the string that you pass is NaN (not a number) and will therefore throw the error on line 3.

The way the error was displaying, I thought it was a problem with the syntax of 'Both must be numbers.' Thanks for your help.

But shouldn't it run to a certain point when the isNaN is true? For example get the first two lines in the console done, then run the error?