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:
24
random-number-range/app.js
Normal file
24
random-number-range/app.js
Normal 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}.`);
|
11
random-number-range/index.html
Normal file
11
random-number-range/index.html
Normal 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>
|
Reference in New Issue
Block a user