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

I got my code to work with alert() + error message. Just want to know if I've missed anything or it could be better.

Here's my code. It's working properly. Just want to know if I've missed anything or if it could be coded better. Thanks!

// Ask for the upper & lower numbers
var upperCall = prompt("We need two values for a random number. What is your higher number value?");
var lowerCall = prompt("Now, what is your lower number value?");

// Random number function with error message
var getRandomNumber = function(lower, upper) { // works with parameters
  var upper = parseInt(upperCall);
  var lower = parseInt(lowerCall);
  if (isNaN(upperCall) || isNaN(lowerCall) ) {
    alert("Please type in a number.");
    throw new Error('error message');  
  } else {
    return Math.floor(Math.random()*(upper-lower)+1)+lower;
  }
}

// Results on browser
document.write('Your random number is ' + getRandomNumber() + '.');

1 Answer

Hello

try setting

upperCall and lowerCall to a null values and see what happens.

you should check for null before doing something like this:

var upper = parseInt(upperCall);

var lower = parseInt(lowerCall);