I have this problem with the try , catch and throw i errors. But the result am getting is way of mark. this is the code. when i type number greater than 5 into the form field, instead of throwing too high nothing displayed. And when i typed in numbers less than 5, it shows too high, instead of too low. In fact most of the other if conditions does'nt display. except. the empty and too high. can somebody explain which such anomally?
function myFunction() {
var x, message;
message = document.getElementById('para');
message.innerHTML = '';
x = document.getElementById('formField').value;
try {
if (x == "") throw 'empty';
if (x == NaN) throw 'not a number';
x = Number(x);
if (x < 5) throw 'too low';
if (x > 5) throw 'too high';
}
catch(err){
message.innerHTML = 'input is ' + err;
}
}
many of you guys don't seem to understand my problem here this code was a direct copy and paste from W3schools. but its not working normally. the various if statements are supposed to throw certain errors when the value from the input match the if statment. but rather it is kind of giving a disjointed result. like when i typed in just 2 numbers, instead of throwing input is too low, i throws input is too high. and when i typed in numbers more than 5, it throws no error, and for the NaN no response from javascript.
is there anybody out there that can understand what am talking about? i think you should actually run the code yourselves and please educate me where am confuse.