var numbers = ["1", "2", "3"]; var new_numbers = numbers.map(parseInt);However, this will give you unexpected output of:
[ 1, NaN, NaN ]Why? Well, this is because “map” passes more arguments into the callback function:
callback(item, index, array);This works fine if your callback function only accepts one arguments, which will be “item”. However, “parseInt” function receives two arguments:
parseInt(string, base)So in our case the actual value of “index” got interpreted as “base” in the parseInt function, and of course it will give you very unexpected result. The solution is to create a small function which wraps the “parseInt”:
var numbers = ["1", "2", "3"]; var new_numbers = numbers.map(function(x) { return parseInt(x, 10) })This should get you green lights in your test cases :).