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

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

i may be off the track here from what the challenge is , but is this correct ?

function getRandomNumber( lower, upper ){
  if (isNaN(lower) || isNaN(upper)){
  throw new Error ('One of the arguments input is not a number ,please check!');
  } else { console.log ( getRandomNumber(lower, upper) );
  }
  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

If you watch the next video, an example solution is presented. You'll see there's no "else" involved in the example.

Furthermore, calling the function from within itself without changing the arguments would set up a recursive loop that would likely lock up the browser while it consumes memory resources until they are depleted (did you try running it?).

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

Yes I ran the code, but dint work so I saw the sol vdo. And removed "else" clause.rest it worked fine.