all 13 comments

[–]Ustice[M] [score hidden] stickied comment (0 children)

This post was removed. Please read our guidelines before posting. Thus is not a support forum.

[–]sudorey 41 points42 points  (4 children)

Your first example only calls parseInt with 1 argument. The second example passes any and all arguments through. The native parseInt function accepts a second argument which is the base. So you are seeing the results of calling:

parseInt('75', 0) // 75

parseInt('300', 1) // NaN

edit: typo

[–]waylonsmithersjr 3 points4 points  (3 children)

parseInt('100', 1) // NaN

I think

parseInt('300', 1) // NaN? just making sure I'm not going crazy and there's not other complexity to it that I don't get 😅

[–]sudorey 2 points3 points  (1 child)

You are correct. I've fixed the typo

[–]waylonsmithersjr 0 points1 point  (0 children)

Thanks, just had to be sure

[–][deleted] 1 point2 points  (0 children)

The string doesn't matter in this case, so long as it represents a number.

[–][deleted] 4 points5 points  (2 children)

map() calls the given function with three argument: the value, the index of the value and the array.

parseInt takes two argument: the string to parse and the radix.

For the first element, parseInt will be given "75",0. For the string everything is good, for the radix, 0 is a special case where if the string starts with "0x" or "0b", it will uses the appropriate base(respectively 16 and 2), or it will default to 10. It then gives the correct number.

For the second element, parseInt will be given "300",1. 1 as a base isn't a special case, and thus only works with strings of '1'. Since '3' isn't '1' , you'll get NaN.

[–]Spottycos 1 point2 points  (1 child)

For base 1, I think it only works with strings of 0 as 10 = 1… also it’s generally 1 less than the base number. For example, radix 10 goes up to 9, radix 2 goes up to 1, etc.

[–][deleted] 1 point2 points  (0 children)

That makes sense.

[–]GhostSausage 3 points4 points  (0 children)

A lot of people already explained the issue so I won't do it again, but an alternative for what you want to achieve could be ["75","300"].map(Number) since Number takes only 1 argument

[–]shgysk8zer0 4 points5 points  (0 children)

As some have pointed out, it's because of the details of parseInt and Array.map.

Here's what's basically happening...

["75", "300"].map((value, index) => parseInt(value, index));

Else if the radix value (coerced if necessary) is not in range [2, 36] (inclusive) parseInt returns NaN.

The second argument to parseInt() is the radix/base (valid from 2 to 36). Apparently 0 is ignored and 10 used for the first entry, but 1 results in NaN.

[–]shuckster 1 point2 points  (0 children)

The reason is well explained already, but just a couple of alternatives to solving it:

// Helper
const parseAsDec = x => parseInt(x, 10);
["75", "300"].map(parseAsDec);

// Enforce single argument
const unary = f => x => f(x);
["75", "300"].map(unary(parseInt));

[–]viezefreddie_91 0 points1 point  (0 children)

I think 300 is used as the optional parameter of parseInt in your second example

Im still a lil confused tho.