0

Why is my typeof function not detecting numbers right?

https://code.sololearn.com/WIS31gD6py0U/?ref=app

20th Apr 2017, 5:56 PM
AceDev
4 Answers
+ 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"); } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20th Apr 2017, 6:21 PM
Maz
Maz - avatar
+ 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"
20th Apr 2017, 6:17 PM
Rrestoring faith
Rrestoring faith - avatar
+ 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"); }
20th Apr 2017, 6:17 PM
Tim G
Tim G - avatar
+ 2
Thank you very much all!
20th Apr 2017, 6:54 PM
AceDev