-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add explanation for the output of
parseInt
with different inputs
- Loading branch information
1 parent
ac44eb2
commit c15de4c
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# 77. parseInt 2 | ||
|
||
### Problem | ||
|
||
https://bigfrontend.dev/quiz/parseInt-2 | ||
|
||
# | ||
|
||
### Problem Description | ||
|
||
What does the code snippet below output by `console.log`? | ||
|
||
```js | ||
console.log(parseInt(0.00001)); | ||
console.log(parseInt(0.000001)); | ||
console.log(parseInt(0.0000001)); | ||
console.log(parseInt('0x12')); | ||
console.log(parseInt('1e2')); | ||
``` | ||
|
||
# | ||
|
||
### Answer | ||
|
||
```js | ||
console.log(parseInt(0.00001)); // 0 | ||
console.log(parseInt(0.000001)); // 0 | ||
|
||
console.log(parseInt(0.0000001)); // 1 | ||
// If the input value is less than or equal to 1e-7 (0.0000001), `parseInt()` | ||
// returns `1`. | ||
|
||
console.log(parseInt('0x12')); // 18 | ||
// If the input string starts with `"0x"`, `radix` is assumed to be `16`. | ||
// (12)₁₆ = (1 × 16¹) + (2 × 16⁰) = (18)₁₀ | ||
|
||
console.log(parseInt('1e2')); // 1 | ||
// Since `"1e2"` is a string, `parseInt()` does not recognize exponential notation | ||
// and thus stops at the first letter. | ||
``` | ||
|
||
# | ||
|
||
### Reference | ||
|
||
- [parseInt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) | ||
|
||
- [parseInt('1e1') vs parseFloat('1e1')](https://stackoverflow.com/questions/31732983/parseint1e1-vs-parsefloat1e1) |