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

Testing for Number Arguments

function getRandomNumber(x, y = 100) { if (isNaN(x) || isNaN(y)) { throw Error('Both arguments must be numbers.'); } else if(x > y) { return Math.floor(Math.random() * (x - y + 1)) + y; } else {

return Math.floor(Math.random() * (y -x + 1)) + x; } } // Call the function and pass it different values

//console.log(getRandomNumber(4, 'six')); console.log(getRandomNumber(4, 6)); console.log(getRandomNumber(4, 55)); console.log(getRandomNumber(4, 'six'));

The code above works but when i go to inspect elements, the first console.log generates an error and the others do not generate any results. When i place it last however, the others console.logs produce results. Why is that so? Thank you.

1 Answer

Steven Parker
Steven Parker
231,261 Points

Anytime an error is thrown (and not caught), the script stops running. This is normal behavior.

In other courses you will learn how to use the "try" and "catch" statements to report errors but continue to run the script.