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

andres vargas
andres vargas
3,147 Points

Why is not showing the error message??

function alea(n1,n2) { if( isNaN(n1) || isNaN(n2) ){ throw new Error('Ambos argumentos deben ser numeros'); } return Math.floor(Math.random() * (n2 - n1 + 1)) + n1; }

console.log(alea(40,50)); console.log(alea('9',11)); console.log(alea(50,60));

Hi Andres,

I tested your code in the console, and it works as it should. Can you please post a snapshot of your workspace for us to see? If you don't know how to do that, please check out this forum post: http://www.teamtreehouse.com/forum/workspace-snapshots

2 Answers

Hey Andres,

The reason why the error is not thrown is because of the way isNaN works. isNaN converts strings into numbers and then tests them to see if they are not a number. So, the string '9' gets converted to a number and so isNaN returns false. But if the string contains any non numerical characters isNaN will be unable to convert the string into a number and isNaN will return true.

Here is an article from the Mozilla Developer Network about isNaN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

andres vargas
andres vargas
3,147 Points

so whats the solution? because i use the same code from the video (random number challenge, part II) as the teacher.

You inputted the number 9 as a string, but the instructor spelled out the word nine. They are very different things, Andres.

andres vargas
andres vargas
3,147 Points

ohhhh, got it! Thank you very much :)

You're welcome, Andres!

Allison Davis
Allison Davis
12,698 Points

I think you need to wrap the second part of the function - return Math.floor(Math.random() * (n2 - n1 + 1)) + n1; - in an 'else' clause:

function alea(n1,n2) { if( isNaN(n1) || isNaN(n2) ){ throw new Error('Ambos argumentos deben ser numeros'); } else { return Math.floor(Math.random() * (n2 - n1 + 1)) + n1; } }

You don't have to put an else clause here, because the return command only fires off if an error is not thrown.

You're welcome, Allison. Keep in mind that anything that stops execution of a script can be followed by an immediate return statement. For example, in the code below, the script will stop if the number 5 is an input in the function, but for every other number, it will return the number times 2. So, trying running this in your console:

function myfunc (num) {
if (parseInt(num) === 5) {
//returns always stop execution of the function
//you can also throw an error but that stops execution of the script
return false;
}
//notice no else clause here
return num * 2;
}
//will log false
console.log(myfunc(5));
//will log 4
console.log(myfunc(2));
//will log 8
console.log(myfunc(4));