$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #257

Which fix removes the bug?

const qty = 0.0000005;
const whole = parseInt(qty);
console.log(whole); // expected 0, prints 5
  1. Truncate with Math.trunc(qty)
  2. Use parseInt(qty, 10)
  3. Use Number.parseInt(qty)
  4. Use parseInt(String(qty))
parseInt converts its argument to a string first, and 0.0000005 becomes "5e-7", whose leading digits are 5. Math.trunc works on the number.
Play Problem Solving ▸