Add a simple random number generator that allows user input for the range and prints the result to the browser console.

This commit is contained in:
2025-08-14 11:57:23 -04:00
parent 7f42ae04b2
commit 48ade4eef6
2 changed files with 35 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
// 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);
if ( Number.isInteger(promptInt) || Number.isInteger(promptIntHigh) ) {
console.log(promptInt);
console.log(promptIntHigh);
} else {
console.log(`Error: One or more of the provided inputs is not a number.`);
}
// Use Math.random() and the user's number to generate a random number
const resultNumber = Math.floor(Math.random() * promptIntHigh) + promptInt;
// Create a message displaying the random number
console.log(`A random number between ${promptInt} and ${promptIntHigh} is ${resultNumber}.`);

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Random Number Range</title>
</head>
<body>
<main></main>
<script src="app.js"></script>
</body>
</html>