I was resolving some problems on codewars and tried to convert a string to a list of numbers like this:
"102904".split("").map(parseInt);
The expected result would be something like this:
[1, 0, 2, 9, 0, 4]
But it returns instead:
[1, NaN, NaN, NaN, 0, 4]
At believe that map should apply to each element in the list which are strings of 1 digits. One could believe that it isn't parsing correctly because the base isn't used but:
"102904".split("").map(function(x){ return parseInt(x);});
[ 1, 0, 2, 9, 0, 4]
Using parseInt(x, 10)
, doesn't change the result. But sending directly parseInt to map creates NaN
...
I tried on Chrome and Firefox and I receive the same results.
parseInt
involves second argument - radix.
Your problem is that - map
sends to arguments: item itself and index.
Problem digits are: 0, 2, 9
Why ?
NaN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With