Created
July 9, 2012 19:38
-
-
Save kurtbrose/3078448 to your computer and use it in GitHub Desktop.
javascript parseInt(Infinity) lolwut
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1/0 | |
Infinity | |
parseInt(1/0, 5) | |
NaN | |
parseInt(1/0, 19) | |
18 | |
parseInt(1/0, 20) | |
18 | |
parseInt(1/0, 45) | |
NaN | |
parseInt(1/0, 22) | |
18 | |
What an evil interview question to ask WTF is going on here... | |
Here's wtf is going on: | |
parseInt is coercing Infinity to the string "Infinity; | |
then, apparently JS will extend hex notation, so the digits of base 19 are 0123456789ABCEFGHI | |
parseInt parses as much as it can (1 digit, 'I') whose value is 18. | |
Here by passing some different strings, the behavior becomes clear: | |
parseInt("H", 19) | |
17 | |
parseInt("G", 19) | |
16 | |
(In a way you are asking for it: what else is the function supposed to do when asked to parse integers of a base greater than 16? | |
Well, probably it should error out...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"Evil" is correct!