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

Andrew Whatmore
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrew Whatmore
Full Stack JavaScript Techdegree Student 1,292 Points

My solution - allows for user entering in reverse order

A solution that allows for the user entering the numbers in the reverse order ie the highest number first.

Rather than providing an error if the first number is highest, it will work out the minimum and maximum and then calculate the random number.

I left in my original solution for the condition, although it is longer than the ones provided by Treehouse.

Adding to a page using textContent is more secure than innerHTML according to MDN.

// Collect input from a user & Convert the input to a number
const userNumber1 = parseInt(prompt('Enter a number'));
const userNumber2 = parseInt(prompt('Enter another number'));

// Create min and max variables
let minimum;
let maximum;

// assign the minimum and maximum
if (userNumber2 > userNumber1) {
  minimum = userNumber1;
  maximum = userNumber2;
} else {
  minimum = userNumber2;
  maximum = userNumber1;
}

// Create a div to display the message later
let div = document.createElement("div");

// if minimum and maximum are both numbers that are not 0 OR minimum is 0 (meaning NaN's only are excluded)
if ((minimum && maximum) || (minimum === 0)) {
  // Use Math.random() and the user's number to generate a random number
  const randomNumber = Math.floor(Math.random() * ((maximum - minimum) + 1) + minimum);
  div.textContent = `${randomNumber} is a random number between ${minimum} and ${maximum}`;
  }
else {
  div.textContent = `You need to provide a number. Please try again.`;
}

// add message div to the <main> section of the page
document.querySelector('main').appendChild(div);

2 Answers

Steven Parker
Steven Parker
243,306 Points

If you look at the other posts about this video you will find a number of proposed enhancements, but auto-correcting is one of the best. And you're quite right about the enhanced security of textContent when HTML features are not required.

Since the workspace tasks are generally just enough to illustrate the lesson principles, you will no doubt find many opportunities to expand and improve on the lesson projects.

Good job! :+1:

Andrew Whatmore
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrew Whatmore
Full Stack JavaScript Techdegree Student 1,292 Points

Thanks Steven.

There is a case where my condition doesn't work - if a non-number is entered first followed by 0, it will try to create the random number using NaN. The condition if (minimum === 0) is not stringent enough.

So best to use one of the provided solutions which do work:

if ( lowNumber >= 0 && highNumber ) {

// ...or...

if ( isNaN(lowNumber) || isNaN(highNumber) ) {