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 Numbers The Math Object Random Number Challenge – Solution

Alex Hort-Francis
Alex Hort-Francis
17,074 Points

My solution to the two-prompt random number generator

This one was a bit trickier, but this code seems to do the trick! :)

// Collect input from a user & convert to a number
const lowNumber = +prompt("Gimme a low number");
const highNumber = +prompt("Gimme a high number");

// Check high number is higher than low number
if (highNumber <= lowNumber) {
  alert("that number is lower than the first one!");
}

// Use Math.random() and the user's number to generate a random number
const difference = highNumber - lowNumber;
const random = Math.floor(Math.random() * difference) + lowNumber;

// Create a message displaying the random number
console.log(`Your random number between ${lowNumber} & ${highNumber} is: ${random}`);

1 Answer

I think this is a great solution, keep up the hard work!