0
Why is my typeof function not detecting numbers right?
4 Respostas
+ 12
Because the prompt() function converts the output in string.
Try with this, in my CodePlayground works well! ^_^
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var num = prompt("Enter something");
if (!isNaN(parseFloat(num))) {
alert("it's a number");
} else {
alert("It's not a number");
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ 9
Prompt returns a string. So no matter what you put in the text box, 'num' would be a string.
ex/
input : 7
then
num = "7"
+ 6
@ValentinHacker No. Why do you post this when you didn't even try it??
prompt always returns a string, so you have to convert num to a number and then check if it is NaN.
var num = Number(prompt("Enter a Number"));
if(isNaN(num)){
alert("It's not a number");
}
else{
alert("It's a number");
}
+ 2
Thank you very much all!