My range does not seem to be working. E.g. if I input the smallest number to be 10, and the biggest number to be 20, it would not give me within the range. Any idea how to fix this?
var randomNo;
function playFunction() {
message1 = document.getElementById("wrongInput");
message1.innerHTML = "";
message2 = document.getElementById("wrongInput2");
message2.innerHTML = "";
x = document.getElementById("input").value;
x2 = document.getElementById("input2").value;
try {
if (x == "")
throw "empty";
if (isNaN(x))
throw "not a number";
} catch (err) {
message1.innerHTML = "Input is " + err;
message1.style.color = "red";
x = Number(x);
}
try {
if (x2 == "")
throw "empty";
if (isNaN(x2))
throw "not a number";
x2 = Number(x2);
} catch (err) {
message2.innerHTML = "Input is " + err;
message2.style.color = "red";
x2 = Number(x2);
}
try {
if (Number(x) > Number(x2))
throw "must be higher than the lowest number";
} catch (err) {
message2.innerHTML = "The input " + err;
}
var min = document.getElementById("input").value;
console.log(min);
var max = document.getElementById("input2").value;
console.log(max);
randomNo = Math.floor(Math.random() * (max - min + 1))
console.log(randomNo);
}
<html>
Enter a smaller number<br>
<input id="input" type="text">
<span id="wrongInput"></span><br> Enter a larger number<br>
<input id="input2" type="text">
<span id="wrongInput2"></span><br>
<button type="button" onclick="playFunction()">Play button</button>
<br>
</html>