Skip to content

Commit ea5d305

Browse files
committed
tricky question on parseint
1 parent 331ffd0 commit ea5d305

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Javascript/Tricky-JS-Problems/Collection-of-Tricky-JS-Questlions.md

+10
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,13 @@ Therefore it behaves as if you called `parseInt("I", 19)`, which converts to dec
311311

312312
[1]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Infinity
313313
[2]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt
314+
315+
### Explanation-2
316+
317+
Here's the sequence of events:
318+
319+
- `1/0` evaluates to `Infinity`
320+
- `parseInt` reads `Infinity` and happily notes that `I` is 18 in base 19
321+
- `parseInt` ignores the remainder of the string, since it can't be converted.
322+
323+
Note that you'd get a result for any base `>= 19`, but not for bases below that. For bases `>= 24`, you'll get a larger result, as `n` becomes a valid digit at that point.

0 commit comments

Comments
 (0)