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 trialCristian Pintado
Full Stack JavaScript Techdegree Student 1,881 PointsRandomNumber
This is how a wrote my program without if and else statement. Is this okay?
// Collect input from a user const number1 = prompt('Please enter a high number'); const number2 = prompt('Please enter a lowest number');
// Convert the input to a number const highNumber = parseInt(number1); const lowestNum = parseInt(number2);
// Use Math.random() and the user's number to generate a random number if( lowestNum && highNumber){ const randomnumber = Math.floor(Math.random() * highNumber) + lowestNum;
// Create a message displaying the random number
console.log(${randomnumber} is a random number between ${lowestNum} and ${highNumber}.
);
}else{
console.log('Please enter two numbers. Try again.');
}
3 Answers
Indiya Gooch
12,075 PointsA creative solution!
The only problem you'll encounter is the first console.log statement as you need to use interpolation (with backticks - "`") to use dynamic values (ei, variables, math statments,ect):
console.log( ` ${randomnumber} is a random number between ${lowestNum} and ${highNumber}. ` );
const number1 = prompt('Please enter a high number');
const number2 = prompt('Please enter a lowest number');
const highNumber = parseInt(number1);
const lowestNum = parseInt(number2);
if( lowestNum && highNumber)
{
const randomnumber = Math.floor(Math.random() * highNumber) + lowestNum;
console.log(`${randomnumber} is a random number between ${lowestNum} and ${highNumber}.` );
}
else{ console.log('Please enter two numbers. Try again.'); }
Cristian Pintado
Full Stack JavaScript Techdegree Student 1,881 Pointsthank you
Joseph Dwumfour
2,585 PointsLooking at how the randomnumber is generated isn't there a possibility a generated randomNumber could be outside the range (i.e. greater than the max(highNumber)).