You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the "Conditional execution" section of Chapter 2:
vartheNumber=prompt("Pick a number","");if(!isNaN(theNumber))alert("Your number is the square root of "+theNumber*theNumber);
With this modification, if you enter “cheese”, no output will be shown.
The string "cheese" is surely "not a number". But "not a number" has a specific meaning for numbers represented as IEEE-754 floating-point values. This is confusing.
I suggest replacing the isNaN function with the typeof operator (typeof "cheese" !== 'number'). Or at least use the Number.isNaN() function in ES6:
I am not trying to use isNaN as a typeof equivalent here (after all, prompt will never return a number). Rather, I'm testing whether the string holds a valid number. For some reason, the explicit Number conversion that I used to have here got dropped in an apparently unrelated patch (d7c72a4). Not sure what I was doing there, but attached patch restores it.
Activity
Restore explicit Number conversion in square root example
marijnh commentedon Dec 14, 2014
I am not trying to use
isNaNas atypeofequivalent here (after all,promptwill never return a number). Rather, I'm testing whether the string holds a valid number. For some reason, the explicitNumberconversion that I used to have here got dropped in an apparently unrelated patch (d7c72a4). Not sure what I was doing there, but attached patch restores it.