Hello, everyone, I am the front-end watermelon brother, this article is about a more classic front end questions, this article will be in-depth analysis of this topic, deep knowledge, so that you fully grasp the solution of this topic.

This is a Web cliche, and it sucks, but I’m just saying, the gaokao happens every year. This question measures familiarity with JavaScript methods.

Instead of giving the answer, let’s look at the idea.

Array.prototype.map

The first is the array.prototype. map method, which is the Array instance method. Pass a callback to a map, and the map iterates through the array of elements, passing the relevant information to the callback, taking the return value of the callback as an element of the corresponding index of the new array, and returning the new array.

Each time the callback retrieves (1) array elements (2) index values (3) and the array itself for the current iteration.

[1.2.3].map((value, index, array) = > {
	return value * 2;
})
// Return [2, 4, 6]
Copy the code

If you’re a beginner, you might be a little confused about how this black box is implemented, so I’ll just implement the array.prototype. map method.

Array.prototype.myMap = function(callbackfn) {
  const arr = this;
  const retArr = new Array(arr.length);
  for (let i = 0; i < arr.length; i++) {
    retArr[i] = callbackfn(arr[i], i, arr);
  }
  return retArr;
}
Copy the code

parseInt

The array.prototype. map method is actually quite simple; parseInt is much more complicated.

The parseInt function converts a string to a number.

ParseInt (String [, radix]) takes two arguments, the first argument is the string we need to convert. The second argument is the radix radix of the string, for example, 2, which means the string is expressed in binary. It is important to note that the second argument is optional.

If the second parameter is omitted, the situation becomes more complicated. If the string begins with 0x or 0x, it is treated as hexadecimal, otherwise it is treated as decimal.

Here are some examples:

parseInt('10'.2);
/ / = > 2. 1 times 2 plus 0 times 1.

parseInt('0xa');
/ / = > 10. Treated as hexadecimal, equivalent to parseInt('ff', 16)

parseInt('123');
/ / = > 123. The most common use of the decimal system,
Copy the code

Theoretically radix radix should be numbers greater than or equal to 2 and less than or equal to 36, otherwise it will be considered illegal and NaN will be returned. Except for the one value that we’re doing this with, which is 0.

When the radix of parseInt’s second argument is 0, parseInt is passed as the second argument, so the string is treated as decimal.

NaN is also returned when the first non-space character of the string cannot be converted to a number.

Back to the subject

[‘1’, ‘2’, ‘3’]. Map (parseInt) returns three parameters from the map as a callback. JavaScript is a very easily typed language. The missing parameter is set to undefined.

Here parsetInt takes only two arguments, the array element and the index value. That is:

parseInt('1'.0);
// parseInt('1')

parseInt('2'.1);
NaN has no such thing as a base

parseInt('3'.2);
// NaN failed to find a valid character. Although 3 is a number, it cannot be used to express binary, which can only be 0 and 1.
Copy the code

So the return result is: [1, NaN, NaN].