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

Joshua Abrams
Joshua Abrams
2,915 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
229,670 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.