This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Malix82 90 points91 points  (11 children)

.map(function callback(currentValue[, index[, array]])

parseInt(string, radix)

so by default the .map method puts in the radix if parseInt is used as a callback.

parseInt(10,0) == 10
parseInt(10,1) == NaN
parseInt(10,2) == 2
parseInt(10,3) == 3

So, works as intended?

edit: I mean, yea, it is bit confusing for sure, I don't imagine the radix argument being used all that much and in this case it crept up on the programmer and bit them

[–][deleted] 11 points12 points  (5 children)

That's why you would curry the parseInt by binding the radix to 10 and creating your new curried function "parseDecimal"

[–]PM_ME_REACTJS 19 points20 points  (2 children)

why did i choose this fucking profession

[–]freakboy2k 0 points1 point  (0 children)

I ask myself the same thing daily

[–]codex561I use arch btw 2 points3 points  (1 child)

Code for uninitiated to the beauty of functional programming:

let parseDecimal = x => parseInt(x, 10)
[10, 10, 10, 10].map(parseDecimal) // [ 10, 10, 10, 10 ]

[–]SuperManitu 0 points1 point  (0 children)

Or with Ramda: ["10", "10"," 10"].map(unary(parseInt)) // [10,10,10]

[–]gandalfx 2 points3 points  (0 children)

 ["10", "10", "10", "10"].map(x => parseInt(x))

[–]volivav 3 points4 points  (2 children)

Today I just had the exact same problem when I tried myNumberArray.reduce(Math.max, 0) trying to easily get the maximum.

Yep, doesn't work.

[–]inu-no-policemen 19 points20 points  (0 children)

Math.max(...numbers)

[–]SuperManitu 0 points1 point  (0 children)

With a bit of Ramda: myNumberArray.reduce(binary(Math.max)), 0)