Skip to content

Commit

Permalink
feat: Add explanation for the output of parseInt with different inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaban-Eissa committed Jun 9, 2024
1 parent ac44eb2 commit c15de4c
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Questions-Answers/77.parseInt-2.md
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)

0 comments on commit c15de4c

Please sign in to comment.