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

Brian Hoeynck
Brian Hoeynck
10,173 Points

A few questions about my code...

So for this challenge, I tried to push myself some and have the random number generator spit out a random number between two user-supplied numbers. The first part looked like this:

var lowerNum = prompt("OK, what's the lower number?");
lowerNum = parseInt(lowerNum);
var higherNum=prompt("And the higher number?");

Next, I added the function like Dave did in the videos and it looked something like this:

function getRandomNumber(lower, upper) {
    if(isNaN(lowerNum) || isNaN(higherNum)) {
        throw new Error("Error Message - Input is NaN.")
    var randomNumber=Math.floor(Math.random()*(upper-lower+1)+lower);
    return randomNumber;

document.write(getRandomNumber(lowerNum, higherNum));

My question is this: for my situation, is the "if" statement where we throw an error necessary? Since I used prompts to collect user input, and prompts return strings, I used the parseInt() function to make sure that I'm working with numbers. Would the parseInt() function catch it if somebody entered "ten" instead of 10? Or are there scenarios where the isNaN( ) function still pretty important?

Thanks for your help, and let me know if there is somewhere else I could improve too.

2 Answers

Hi Brian,

Yes, it's necessary because the parseInt() function isn't guaranteed to return a number. The string you posted, "ten", is one example of a string that will result in parseInt returning NaN instead of a number.

parseInt will return NaN whenever the very first character in a string can not be converted to a number. In the case of "ten", "t" can't be converted to a number and so NaN is returned.

The following strings would all result in NaN: "ten", "t123", ".123", "alsjksfjk"

The following are examples that will result in a number:

parseInt("123"); // 123
parseInt("123.45"); // 123
parseInt("123hello"); // 123

Since you don't know what the user will type, you can't be sure they will type a string that can be successfully converted to a number.

David Kaneshiro Jr.
David Kaneshiro Jr.
29,247 Points

Hi Brian Hoeynck,

The isNan checks aren't really necessary for your current program, but they are good to have if you or someone else re-uses the getRandomNumber function in another program that doesn't necessarily get its values from the parseInt function. It is usually good practice to write your functions so that you can check if any arguments passed to the function are what your function expects. Since the getRandomNumber function expects two number values it is a good idea to make sure that the arguments lower and upper are numbers.