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 – Two Numbers Solution

Bjørn Jakobsen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Bjørn Jakobsen
Front End Web Development Techdegree Graduate 17,003 Points

My solution with a second validation to check if the high number is higher than the low number

// Collect input from a user
const lowUserNumber = prompt('Please provide your lowest number.');
const highUserNumber = prompt('Please provide your highest number.');

// Convert the input to a number
const lowNumber = +lowUserNumber;
const highNumber = +highUserNumber;

console.log(lowNumber);
console.log(highNumber);

if(lowNumber && highNumber ){
  if ( (highNumber > lowNumber) ) {
    // Use Math.random() and the user's number to generate a random number
    const randomNumber = Math.floor( Math.random() * (highNumber - lowNumber + 1) ) + lowNumber;

 // Create a message displaying the random number
    document.querySelector('main').innerHTML = `${randomNumber} is a random number betwwn ${lowUserNumber} and ${highUserNumber}`;
  } else {
    document.querySelector('main').innerHTML = `The second number has to be higher than the first. Try again.`
  }

} else {
  document.querySelector('main').innerHTML = `You need to provide two numbers. Try again.`
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

When posting code, use Markdown formatting to preserve the code's appearance and retain special symbols.

But a nice enhancement to the example program. You'll probably think of many ways to improve the examples in future lessons.