Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Numbers The Math Object Random Number Challenge – Two Numbers Solution

Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 13,822 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
217,519 Points

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

Steven Parker
Steven Parker
217,519 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 13,822 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>`;

}