diff --git a/Questions-Answers/77.parseInt-2.md b/Questions-Answers/77.parseInt-2.md new file mode 100644 index 0000000..72a4299 --- /dev/null +++ b/Questions-Answers/77.parseInt-2.md @@ -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)