Stranger Things, JavaScript Edition
Scenario #1: [‘1’, ‘7’, ‘11’].map(parseInt)

Let’s take a look at the code for our first scenario

['1', '7', '11'].map(parseInt);

For what you would expect the output to be:

[1, 7, 11]

However, things get a bit off here, and the actual result is:

[1,NaN,3]

At first, this may look up very weird, but it actually has an elegant explanation. To understand what is going on, we need to understand the 2 involved functions, map and parseInt .

map()

map() calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values (including undefined).

Now the callback function referenced above will receive some particular parameters, let's take an example with it's output:

[1, 2, 3].map(console.log)

1 0 > (3) [1, 2, 3]

2 1 > (3) [1, 2, 3]

3 2 > (3) [1, 2, 3]

As can be seen, the map function not only did pass the value of the item but also the index and a copy of the full array on each iteration. This is important and is in part what’s affecting our previous result.

parseInt()

The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

So now, by definition, parseInt(string [, radix]) expects two parameters, the string we want to parse, and the radix.

Resolving the mistery

Now we know enough about the two functions, let’s try to understand what is happening in our case, we will start with our original script, and we will explain it step by step:

['1', '7', '11'].map(parseInt);

As we know the callback for the map function will receive 3 arguments, so let's do that:

['1', '7', '11'].map((currentValue, index, array) => parseInt(currentValue, index, array));

Starting to get an idea of what happened? When we add the arguments, it comes clear that the parseInt function is receiving additional parameters and not only the actual value of the item in the array, so now we can take test what the function would do for each of these value combinations, but we can also ignore the array parameter as it will be discarded by the parseInt function:

parseInt('1', 0)

1 parseInt('7', 1)

NaN parseInt('11', 2)

3

So that now explains the values we saw initially, the parseInt function result is being altered by the redix parameter which determines the base for the conversion.

Is there a way to get the originally expected result?

Now know how it works, we can easily fix our script and get the desired result:
['Juan Cruz Martinez']
['Nodejs', 'JavaScript', 'Software Development', 'Startup', 'Programming']