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

Please can someone look at this code and tell me why the error string is not showing in the console

/* test your new skills in functional program by creating a random number generating function. */

function randomGenerator (lower , upper) { if (isNaN(lower)|| isNaN(upper)) { throw new Error("Both fields have to be numbers not words");

}

var randomnumber = Math.floor(Math.random()*( upper - lower + 1 )) + lower ;

return randomnumber;

}

console.log(randomGenerator(45,565)) console.log(randomGenerator(5 ,46)) console.log(randomGenerator(15,545)) console.log(randomGenerator(435,955)) console.log(randomGenerator(145,955))

Tim Strand
Tim Strand
22,458 Points

Try opening your developer tools (F12) i believe you have multiple syntax errors. Also reference the Markdown Cheatsheet for correctly formatting the code you post here so that we can effectively help you.

2 Answers

As you wrote in your code :

if( isNaN(lower) || isNaN(upper) { throw new Error ("Both fields have to be numbers, not words");}

If condition evaluates to true if either "lower" or "upper" argument is not a number.

Now in order to make this condition true you will have to pass in some erroneous values, but you're simply passing valid numbers in console log that's why the above if condition is not evaluating to true.

So in order to make it work, try to do something like,

console.log( randomGenerator( "A" , 2 ) );

or

console.log( randomGenerator( 2 , "A" ) );

or

console.log( randomGenerator( "A" ,"B" ) );

I hope it answers your question, Happy Coding !

Check your syntax: brackets

function randomGenerator (lower , upper) { if (isNaN(lower)|| isNaN(upper)) { throw new Error("Both fields have to be numbers not words");} else{var randomnumber = Math.floor(Math.random()*( upper - lower + 1 )) + lower ; return randomnumber; } }

console.log(randomGenerator(45,565)) console.log(randomGenerator(5 ,46)) console.log(randomGenerator("a",545)) console.log(randomGenerator(435,955)) console.log(randomGenerator(145,955))