Files
trinkets/random-number-range/app.js

24 lines
892 B
JavaScript
Raw Normal View History

// Collect input from a user
const promptInput = prompt(`Give me the low number for your range.`);
const promptInputHigh = prompt(`Give me the high number for your range.`);
console.log(promptInput);
console.log(promptInputHigh);
// Convert the input to a number
const promptInt = parseInt(promptInput);
const promptIntHigh = parseInt(promptInputHigh);
2025-08-14 12:03:30 -04:00
if ( Number.isInteger(promptInt) && Number.isInteger(promptIntHigh) ) {
console.log(promptInt);
console.log(promptIntHigh);
2025-08-14 12:03:30 -04:00
// Use Math.random() and the user's number to generate a random number
const resultNumber = Math.floor( Math.random() * (promptIntHigh - promptInt + 1) ) + promptInt;
2025-08-14 12:03:30 -04:00
// Create a message displaying the random number
2025-08-14 12:03:30 -04:00
console.log(`A random number between ${promptInt} and ${promptIntHigh} is ${resultNumber}.`);
} else {
console.log(`Error: One or more of the provided inputs is not a number.`);
}