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

Raul Cisneros
Raul Cisneros
7,319 Points

Here is my attempt at it. If I made mistakes please let me know so I can learn from my mistakes.

function randomNumber(a, b) {
    if (isNaN(a) || isNaN(b)) {
        throw new Error("one or both numbers in your function call is not a number");
    }else{
        var num = Math.floor(Math.random() * (a - b -1) + (b + 1));
    }
    document.write(num); //code test
    return num;
}
console.log(randomNumber(23, "one"));

1 Answer

Matthew Lanin
seal-mask
.a{fill-rule:evenodd;}techdegree
Matthew Lanin
Full Stack JavaScript Techdegree Student 8,003 Points

Hey Raul, it looks good to me.

When I run the code as is, I get the error message that you entered, "one or both numbers in your function call is not a number," which is good because you're passing in a string when you call your function, so the if statement is correctly identifying that it was passed an argument that was not a number.

Then when I change the "one" to a number, a seemingly random number, between the two numbers passed into the function, is logged to my console and written on the page, so the else statement is executing, storing our "random" number in "num", and your function is correctly returning the "num" variable.

So, good work!