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

My solution for second part of challenge

Here's my solution for the second part of the random number generator challenge that sends an error message to the console.

function getRandomNumber ( lower, upper ) {
  if ( isNaN(lower) || isNaN(upper) ) {
    throw new Error('One or more of the values you entered are not numbers.');
  }
  return random = Math.floor(Math.random() * (parseInt(upper) - parseInt(lower) + 1)) + parseInt(lower);
}

var lower = prompt('Type a number.');
var upper = prompt('Type a number higher than the number you just typed.');


console.log(getRandomNumber(lower, upper));

I'd love your feedback. thanks

1 Answer

Hey, your solution looks good and is perfectly valid but one case that you may want to think about is what would happen if you entered a number for your variable 'lower' that is greater than your variable 'upper'. As a general rule, if a user can enter an invalid value they probably will.

Also, you may want to consider returning your error rather than throwing. That way if you do return an error you can enter it into the console.

Hi Ashley Carpenter , that is a great question. I think it might be a good idea to return an error alert if the number they entered in the lower dialog and prompt the user to enter a number that was lower. So if for example they entered 20 when asked to enter a lower number then 10 when asked for a higher number, I'd send out an alert that asked them to enter a value that is higher than 20. But it is a very good question . Thank you. I will update the code and post the changes. I threw the error message because it was what the challenge asked us to do.