Code not working (from W3Schools)

Code not working (from W3Schools)
0

#1
<!DOCTYPE html>
<html>
<body>

<p>Please input a number between 5 and 10:</p>

<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>

<script>
function myFunction() {
    var message, x;
    message = document.getElementById("p01");
    message.innerHTML = "";
    x = document.getElementById("demo").value;
    try { 
        if(x == "")  throw "empty";
        if(isNaN(x)) throw "not a number";
        x = Number(x);
        if(x < 5)    throw "too low";
        if(x > 10)   throw "too high";
    }
    catch(err) {
        message.innerHTML = "Input is " + err;
    }
}
</script>

</body>
</html>

Only the first line in the try code block works for me. The rest display Input is ReferenceError: isNaN is not defined. This displays on all of my browsers if I input anything on the input field.


#2

What are you trying to do?

Presumably isNaN(x) is a function that returns a bool depending on if the passed parameter is a typeof() number.

You are calling the isNaN() function but it is not defined anywhere

NaN stands for ‘Not a Number’


#3

Guess what, it just worked :laughing: Same code and same output as defined in the throw statements. I was trying to define the isNaN() function like you suggested in the if condition. Didn’t work and I set it back to the way it was. And it worked. Maybe a bug in my editor. Cause I have another html file with the same code and it doesn’t work. Crazy.


#4

Oh yeah, i was wrong.

isNaN() is a JS function


#5

Yeah I guess the editor read isNaN() as an undefined function for a minute there. A sign that I need to update or the 32 bit version of VS Code isn’t getting along with my 64 bit computer. Always tripping.