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) Working With Numbers The Random Challenge

My solution, for critique and to help anyone else who might need it.

Solution to Challenge, just in case, open for scrutiny

I'm sure I could've been more efficient in more than one way and I'd love to get feedback. I used Math.round instead of Math.floor because I thought if you wanted to generate a number that included both numbers in the range, than .floor could never give you the max, and .ceil could never give you the min.

var floorNum = prompt("Input a floor number :)");
var ceilNum = prompt("Input ceiling number :)");

var parseFirstNum = parseInt(floorNum);
var parseSecNum = parseInt(ceilNum);

var randNum = Math.round(Math.random() * (parseSecNum - parseFirstNum) + parseFirstNum);

document.write(randNum);

Hi Bryan,

Looks good! My only feedback is that I'd create a function to generate the random number - abstracting away the logic.

function rng(floor, ceil) {
  var floor = floor || 0;
  var ceil = ceil || 1;
  var delta = ceil - floor;
  return Math.round(Math.random() * delta + floor);
}

Cheers!

Colin Marshall
Colin Marshall
32,861 Points

Looks good! You might also want to add a conditional to check to make sure that the floor number is less than the ceiling number, and you could also put it in a function like Robert Richey's solution.