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 trialJeremi Watabiki
4,833 PointsIs the 'else' clause necessary to return the random number generated?
function getRandomNumber( lower, upper ) {
if ( isNaN(lower) || isNaN(upper) ) {
throw new Error ('You did not enter a valid number.');
}
else {
return Math.floor(Math.random() * (upper - lower + 1)) + lower;
}
}
console.log( getRandomNumber( 9, 24 ) );
console.log( getRandomNumber( 1, 100 ) );
console.log( getRandomNumber( 200, 500 ) );
console.log( getRandomNumber( 1000, 20000 ) );
console.log( getRandomNumber( 50, 'one' ) );
2 Answers
Matthew Vega
16,391 PointsNo, but it doesn't hurt anything. You could leave it in or take it out. The function will behave the same way as far as the caller is concerned.
If either isNaN condition is true, the error will be thrown and control will not return to this function. If neither input is NaN, the 'if' block is skipped and the random number will be returned.
Tyler Haas
20,623 Pointsthe else can be omitted since the program would exit if the if statement returns true.