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 trialDamien Lavizzo
4,265 PointsMy Solution (with HTML)
Welp, I completely misunderstood the prompt and went WAY overboard with this one, I thought we had to prompt the user for two numbers and check for validity and everything. Anyway, hope my way over-engineered solution is helpful to someone in the future.
Also if anyone sees anywhere that I could have tightened things up or done something differently I would love the feedback.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get Random Number From Range</title>
</head>
<body>
<script src="randomnumberfromrange.js"></script>
<h2>Please choose two numbers between 1 and 100.<br>
Then we'll pull a random number from between them, mathmagically!</h2>
<p><input type="number" id="firstNumberInput" value="1" min="1" max="100"></p>
<p><input type="number" id="secondNumberInput" value= "1" min="1" max="100"></p>
<p><button id="calculate" onclick="getRandomNumberInRange()">*Cast Randomizio!*</button></p>
</body>
</html>
JS:
function getRandomNumberInRange() {
let firstNumberValue = Number(document.querySelector("#firstNumberInput").value);
let secondNumberValue = Number(document.querySelector("#secondNumberInput").value);
let lowerNumber = Math.min(firstNumberValue, secondNumberValue)
let higherNumber = Math.max(firstNumberValue, secondNumberValue)
let randomNumber = Math.floor(Math.random() * (higherNumber - lowerNumber + 1) + lowerNumber);
console.log(randomNumber)
1 Answer
Steven Parker
231,275 PointsThis is not a criticism of the code, but I did find it interesting that for an exercise in the "Arrow Functions" stage of the course, neither you nor the instructor used any arrow functions in your solutions.