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

piotr kaminski
piotr kaminski
9,484 Points

Why this is not working ?

function getRandomNumber( lower, upper ) {
  if ( isNaN(lower) || isNaN(upper) ) {
      throw new Error('Both arguments must be number');   devtools underline this as a error
  }
  return Math.floor(Math.random() * (upper - lower + 1)) + lower;   
}

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

Mod Edit: Fixed code formatting, see comment for how to do this yourself!

rydavim
rydavim
18,813 Points

When posting on the forums, you can use markdown to format your code and even give it fancy syntax highlighting! Simply wrap your code block in three backticks, and optionally add a language.

```javascript

// Your code here!

```

2 Answers

Steven Parker
Steven Parker
229,695 Points

The line "throw new Error ..." deliberately creates an error, because one of the arguments passed in the first call is 'nine' which is not a number (isNaN).

If you have a JavaScript console open, you should see "Error: Both arguments must be number" displayed there. That message is proof that it is working.

rydavim
rydavim
18,813 Points

Your code is working as expected for me. Try checking the following.

  1. Ensure you're not copy-pasting your code from the developer console. Chrome notes where errors are occurring, but it's not commented out. Make sure if you do copy, that you remove the devtools underline this as a error note.

  2. If you're doing all your console.log testing at the same time like that, keep in mind that once you throw the error you're not going to see the rest of your tests. Try commenting out the intentional failures and see if you get what you're expecting.

If you're still running into issues after that, let me know and we can troubleshoot some more. Your code looks good though, so nice work. Happy coding!