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 Functions Arrow Functions Testing for Number Arguments

Cody Norman
seal-mask
.a{fill-rule:evenodd;}techdegree
Cody Norman
Full Stack JavaScript Techdegree Student 3,421 Points

Number arguments not working quite right

When I input the following into the JavaScript console I get my custom message of "You need to enter a numerical value!" and nothing else, however, if I add in 'lower = 1' the script runs exactly as it should. In the video Guil shows that lower value being blank and the script running just fine. Why is mine different, I actually changed my code from an arrow function to match the video EXACTLY hoping it would work.

function inputNum(lower, upper = 1000 ) { if ( isNaN(lower) || isNaN(upper) ) { throw Error('You need to enter a numerical value!'); } return Math.floor(Math.random() * (upper - lower + 1)) + lower; }

// Call the function and pass it different values

console.log( ${inputNum()} is a random number between 1 and 50 ); console.log(inputNum(75, 600) ); console.log(inputNum(30, 500) );

1 Answer

Steven Parker
Steven Parker
229,732 Points

In the video, the function is named "getRandomNumber", and he passes the value "10" to it to satisfy the "lower" argument.

Here, you are not passing any argument so the "lower" is not a number unless you define a default value.