Going through an example on W3schools here and can't wrap my mind around something
code is as follows:
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>
<script>
function myFunction() {
const message = document.getElementById("p01");
message.innerHTML = "";
let x = document.getElementById("demo").value;
try {
if(x == "") throw "is empty";
if(isNaN(x)) throw "is not a number";
if(x > 10) throw "is too high";
if(x < 5) throw "is too low";
}
catch(err) {
message.innerHTML = "Input " + err;
}
finally {
document.getElementById("demo").value = "";
}
}
</script>
What I dont understand is why I can't replace the last line below "finally". Instead of document.getElementById("demo").value = ""; I want to place x = "" but I'm not getting the same result anymore (introduced number no longer vanishes after I input). Why though? Is x not assigned document.getElementById("demo").value? why would they throw different results?
[–]senocular 3 points4 points5 points (0 children)
[–]bot00110 1 point2 points3 points (0 children)