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

Joshua Abrams
Joshua Abrams
2,239 Points

Would this also be correct?

// Collect input from a user const userNum1 = prompt ("Please enter a low number."); const userNum2 = prompt ("Please enter a high number")

// Convert the input to a number const lowNum = parseInt(userNum1); const highNum = parseInt(userNum2);

if (userNum1 && userNum2) {

// Use Math.random() and the user's number to generate a random number const randomNum = Math.floor(Math.random(userNum1) * userNum2) + 1;

// Create a message displaying the random number

alert(This is you random number ${randomNum})

} else { alert("You need to provide a number please try again.")

}

1 Answer

Steven Parker
Steven Parker
217,557 Points

Combining numbers using logic operators (such as if (userNum1 && userNum2)) is not a reliable way to test for valid numbers. For example, if either number were 0, the test would fail even though 0 is a valid number.

Also, the random function does not take an argument, so passing it the first number doesn't do anything. More importantly, it won't impose any limit on the lowest possible number chosen like the conventional formula will.