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 trialDaniel Statsenko
Full Stack JavaScript Techdegree Student 4,235 PointsWhat if the low number is negative, like -3 for example?
What if the low number is negative, like -3 for example?
1 Answer
Cameron Childres
11,820 PointsHi Daniel,
Take a look at the conditional in the solution:
if (lowNumber >= 0 && highNumber) {
const randomNumber = Math.floor( Math.random() * (highNumber - lowNumber + 1) ) + lowNumber;
console.log(`${randomNumber} is a random number between ${lowNumber} and ${highNumber}.`);
} else {
console.log('You need to provide two numbers. Try again.');
}
lowNumber is checked to be greater than or equal to 0. If it is a negative number this test fails, evaluates as false, and executes the else statement.
You might notice that there's no such test for highNumber. What would happen if you gave a lower value for highNumber than lowNumber, or if you gave highNumber a negative value? For instance lowNumber = 1
and highNumber = -3
. I'd recommend trying this out in your workspace to see the result and brainstorming ways you could test for this.
Hope this helps! Let me know if you have any questions.