I'm trying to use Javascript's higher-order functions for functional programming. However, they are producing unexpected results:
['1','2','3','4'].map(parseInt) // [1, NaN, NaN, NaN]
['1','2','3','4'].map(function(x){
return parseInt(x)
}) // [1, 2, 3, 4]
I expected these two calls to Array.prototype.map
to both return [1, 2, 3, 4]
. How can I map parseInt()
without using an anonymous function?
Similarly, I expected the following two expressions to produce the same value:
[4,6,8,3].reduce(Math.max,-Infinity) // NaN
[4,6,8,3].reduce(function(x,y){
return Math.max(x,y)
},-Infinity) // 8
Also, since I'm new to functional programming in Javascript, please let me know if I'm using any terminology incorrectly.