marijnh / Eloquent-JavaScript

The sources for the Eloquent JavaScript book
https://eloquentjavascript.net
3.02k stars 796 forks source link

Bugs and Errors - Chapter 8 #512

Closed ghost closed 4 years ago

ghost commented 4 years ago

The problem I wish to report is contained within the following code, which is somewhere about halfway down the page.

function promptNumber(question) { let result = Number(prompt(question)); if (Number.isNaN(result)) return null; else return result; }

Number.isNaN("anyString") returns false, which defeats the purpose of this line.

The only way this works is to do something along the lines of if(typeof result != 'number') { return null } else { return result; }

marijnh commented 4 years ago

Number.isNaN("anyString") returns false, which defeats the purpose of this line.

Because result holds the return value from Number, the value passed to Number.isNaN will never be a string.