0

I'm using Lodash...

coffee> _ = require("lodash")
[stuff deleted]

This expression gives the right answer...

coffee> _.map("7-9".split("-"), (x)->parseInt(x))
[ 7, 9 ]

But this one gives something slightly different for the last result in the array:

coffee> _.map("7-9".split("-"), parseInt)
[ 7, NaN ]
coffee>

Why are the answers different? Surely (x)->paresInt(x) should behave the same as parseInt

Salim Fadhley
  • 6,975
  • 14
  • 46
  • 83
  • 4
    Check what parameters are passed to the `_.map` function callback. Then check what parameters `parseInt` accepts. Hint: there are more than one. Hint 2: the actual code to run is `(a, b, c) -> parseInt(a, b, c)` – zerkms Sep 10 '15 at 09:25

1 Answers1

0

Use this instead: require('lodash').map("7-9".split("-"), Number) ;)

https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/parseInt that means parseInt called with two arguments, second, which must be the radix, now is 1 (parseInt(9, 1))

Also you can use https://lodash.com/docs#ary _.map("7-9".split("-"), _.ary(parseInt, 1));

Grigory
  • 411
  • 3
  • 10