0

I have read in many answers that under circumstances radix in parseInt default to 10.

I don't understand though, why I get the output I do below, where the strings

["1856", "1857", "1858"]

get parsed as:

[1856, NaN, 1]

I've set up the example below to check how parseInt responds with different radix values. But none seem to match. What am I missing?

const ids = ["1856", "1857", "1858"];

const parsed = Array.from(new Array(20).keys())
  .reduce((p, n) => ({
    ...p,
    [n]: ids.map(x => parseInt(x, n)),
  }))

console.log(ids.map(parseInt))
console.log(parsed)
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
  • 2
    Why would using `n` as the radix make *any* sense? – Pointy Nov 08 '19 at 16:15
  • Can you share your expected output? – ggorlen Nov 08 '19 at 16:16
  • If you're going to have a "Run code snippet" button appear, you should make sure the code will actually run. – Scott Hunter Nov 08 '19 at 16:17
  • 2
    The problem is that when you pass `parseInt` directly as the `.map()` callback, it will be confused by the second parameter passed by `.map()`. You have to wrap `parseInt()` in a function that ignores the second parameter. – Pointy Nov 08 '19 at 16:18
  • "1857" is the second item in the array. `parseInt("1857", 1)` yields `NaN`. –  Nov 08 '19 at 16:21

0 Answers0