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

Mario Garcia de Leon Recio
Mario Garcia de Leon Recio
3,288 Points

throw new Error(); does not work for me. Anyone can send a tip?

When I run the program, I can not see in my console "error message". I'm using this code:

function getRandomNumber( lower, upper ) {
  if ( isNaN(lower) || isNaN(upper) ) {
    throw new Error ('error message');
  } 
  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 ) );

Look at this row, if ( isNaN(lower) === true || isNaN(upper) === true ) Also you forgot an else statment before return.

Chris Shaw
Chris Shaw
26,676 Points

Hi Mario,

Do you have the developer console in your browser? If not hit F12 which opens it for most browsers otherwise right click on the page and select Inspect from the context menu.

Other than that, your code works without issue for me.

4 Answers

I took this from an answer from the next video (solution video)

function getRandomNumber( lower, upper ) { 
var randomNumberGen = Math.floor(Math.random() * ( upper - lower + 1)) + lower; 
if ( isNaN( lower ) || isNaN(upper) ) { 
try  {
throw new Error("You have to type in a number, not a string you idiot!") 
 } catch (e) {
  console.log(e); 
} 
}else { 
return randomNumberGen; 
} 
}

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

The basics of the "try, catch, throw" element here for it to work in Firefox for me. To throw an error and for it to work use try, catch, throw just like we learned in the "if, else if, else" statement structure.

Steven Parker
Steven Parker
229,783 Points

:+1: It runs fine for me.

My console shows "Uncaught Error: error message", as expected.

And if I comment out the lines with the strings, I get:
6
9168
86

(one particular run)

Mario Garcia de Leon Recio
Mario Garcia de Leon Recio
3,288 Points

Thanks Steven for checking and comment!

I guess there was a problem with my browser / console.

Kyle Vassella
Kyle Vassella
9,033 Points

I was having this same issue. Code was character per character the same as yours, but error doesn't run - instead all of the console.logs successfully run and anything which isn't an integer comes up as NaN.

It worked properly in Safari though. Turned out my current version of Chrome wasn't up to date (but I thought it was). I updated it, but the problem still wasn't fixed. From experience getting buggy workspaces to work I tried a final command+R to refresh the preview and the error code worked!

So to anyone having these bug issues, update your browser to the latest version and after restarting your browser and coming back to preview, hit the preview page with an extra command+R and this may fix the issue.

i used this code and added 'else' statement. why is my console showing nannine for one of the answers ?

function getRandomNumber( lower, upper ) { if ( isNaN(lower) || isNaN(upper) ) { throw new Error ('error message'); } 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 ) );