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

Angel Manuel Couso Jimenez
PLUS
Angel Manuel Couso Jimenez
Courses Plus Student 4,402 Points

My solution, Is Correct?

function getRandomNumber( lower, upper ) {
  if (isNaN(lower) || isNaN(upper)) {
    throw new Error('At least one of the parameters given isnt a numbers');
  } else {
    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 ) );

2 Answers

Steven Parker
Steven Parker
231,275 Points

It looks like you got it. Good job! :+1:

Daniel Salvatori
Daniel Salvatori
2,658 Points

i had almost the same but I wrote the isNan part like this

 if(isNaN(lower) == true || isNaN(upper) == true){

I suppose it is unnecessary but can I write it this way?

Steven Parker
Steven Parker
231,275 Points

You can, but it's never necessary to compare a booean value to "true". Just naming it does the same job.

Angel Manuel Couso Jimenez
Angel Manuel Couso Jimenez
Courses Plus Student 4,402 Points

IsNaN return a Boolean answer so like said Steven compare a Boolean answer with "true" or "false" is kind of redundant, but redundant don't means u can't do it Sorry if I write something wrong am still learning English :)