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

I'm doing exactly the same as Dave, still doesn't run properly.

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

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,723 Points

Hey Damjan,

Your code is working fine. The reason it is not logging all calls to getRandomNumber is that the first call throws the Error you set up in the if clause, stopping the code from continuing to run; so the remaining calls to getRandomNumber never get called.

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

I checked my code over and over and nothing shows up in the console.

function getRandomNumber(lower,upper) { if(isNaN(lower)||isNaN(upper)){ throw new Error('Sorry, please enter a number.'); } 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 ) );

Eshaa Dhall
seal-mask
.a{fill-rule:evenodd;}techdegree
Eshaa Dhall
Full Stack JavaScript Techdegree Student 13,122 Points

The same is happening with my code, I'm using the prompt to enter the value, i even pasred the prompt value to INT but it still gets stuck at the if clause of the function getRandom.

function getRandom(high, bottom){ if(isNaN(high) || isNaN(bottom)) { throw new Error('enter a number.'); }

return Math.floor(Math.random() * (high - bottom + 1)) + bottom;

}

var top=prompt('Enter the High Value'); var low=prompt('enter the bottom value'); var top1=parseInt(top); var low1=parseInt(low); var random_number=getRandom(top1,low1); alert(random_number);