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

Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 15,809 Points

My solution allows for negative numbers.

It doesn't seem to matter if the inputs are low/high or high/low. To allow for negative numbers, I added a test for the input to be >= 0 or <=0.

I chose to output the results to the HTML.

Also, I put the prompt inside parseInt to save a conversion step. I'm not lazy, I'm efficient. It looks similar but is, in fact, different. (No matter what my parents say.)*

// Access the DOM
const main = document.querySelector('main');

// Collect input from a user

const userInputLow = parseInt( prompt("Provide the low number:") ); 
const userInputHigh = parseInt( prompt("Provide the high number:") );

// If the low number is 0, it will test as false
// Added two conditions with a logical or to prevent this

if ( ( userInputLow <= 0 || userInputLow >= 0 ) && userInputHigh ) { 

  const randomNumber = Math.floor( Math.random() * ( userInputHigh - userInputLow + 1)) + userInputLow;

  // Create a message displaying the random number

  main.innerHTML = `
    <h2>${randomNumber} is a random number between ${userInputLow} and ${userInputHigh}</h2>
    `;
} else {
    // userInput tested as false/NaN

    main.innerHTML = 
    `<h2>Required inputs are numbers.  Refresh the page and try again.</h2>`;

}

2 Answers

Steven Parker
Steven Parker
229,744 Points

What if the low number was negative and the high number was 0?   :see_no_evil:

Steven Parker
Steven Parker
229,744 Points

OK, one more suggestion. Instead of testing ranges that cover every possible number, check for not a non-number:

// so instead of this:
if ( ( userInputLow <= 0 || userInputLow >= 0 ) && ( userInputHigh <= 0 || userInputHigh >=0 ) ) {
// try this:
if ( !isNaN(userInputLow) && !isNaN(userInputHigh) ) {
Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 15,809 Points

Ooo... good catch!

Fixed:

// Access the DOM
const main = document.querySelector('main');

// Collect input from a user

const userInputLow = parseInt( prompt("Provide the low number:") ); 
const userInputHigh = parseInt( prompt("Provide the high number:") );

// If the low number is 0, it will test as false
// Added two conditions with a logical or to prevent this

if ( ( userInputLow <= 0 || userInputLow >= 0 ) && ( userInputHigh <= 0 || userInputHight >=0 ) ) { 

  const randomNumber = Math.floor( Math.random() * ( userInputHigh - userInputLow + 1)) + userInputLow;

  // Create a message displaying the random number

  main.innerHTML = `
    <h2>${randomNumber} is a random number between ${userInputLow} and ${userInputHigh}</h2>
    `;
} else {
    // userInput tested as false/NaN

    main.innerHTML = 
    `<h2>Required inputs are numbers.  Refresh the page and try again.</h2>`;

}