2

could someone explain why I get this weird result by executing following code?

'1.0.0.0'.split('.').map(parseInt);

Output:

[1, NaN, 0, 0]
G. Ghez
  • 3,429
  • 2
  • 21
  • 18

1 Answers1

2

parseInt has a second argument that is the radix. map is passing three arguments: currentValue, index and the array. That means that the index of the number is used as the radix. Try this instead:

'1.0.0.0'.split('.').map(function(s) {return parseInt(s);});

https://jsfiddle.net/qbf7u1d7/

BCartolo
  • 720
  • 4
  • 21
  • Thank you for answering and sorry for duplicate... – G. Ghez Dec 11 '15 at 14:24
  • 2
    Alternatively, `'1.0.0.0'.split('.').map(x => parseInt(x))`, if you're working in an environment that supports [ES6 arrow functions](https://www.google.ca/search?q=ES6+arrow+functions&oq=ES6+arrow+functions&aqs=chrome..69i57.1856j0j7&sourceid=chrome&es_sm=91&ie=UTF-8). – user229044 Dec 11 '15 at 14:33