Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky

Interviewer: [‘1’, ‘2’, ‘3’]. What is the result of map(parseInt)? Why?

Me: [1, 2, 3].

Interviewer: You don’t have to come.

Writing in the front

[‘1’, ‘2’, ‘3’]. Map (parseInt) The answer is [1, 2, 3], but the answer is [1, NaN, NaN]. Let’s find out.

ParseInt function

ParseInt (string, radix) converts a string string to integers with radix between 2 and 36. Returns the parsed integer value. NaN is returned if the first character of the parsed parameter cannot be converted to a numeric type.

So far, we have a basic understanding of the parseInt() function. According to the above description, that is, we pass the second parameter of parseInt() a number that is not between 2 and 36, and return NaN. Let’s test this:

console.log(parseInt(10.0)) / / 10
console.log(parseInt(10.1)) // NaN
console.log(parseInt(10.37)) // NaN
Copy the code

According to our thinking, the result here should be 3 nans, but it is not. The radix parameters are clearly explained in MDN:

If the radix is undefined, 0, or unspecified, JavaScript assumes the following: 1. If the input string begins with "0x" or "0x" (a zero followed by a lowercase or uppercase X), the radix is assumed to be 16 and the rest of the string is parsed as a hexadecimal number. 2. If the input string begins with "0" (0), the radix is assumed to be 8 (octal) or 10 (decimal). Which radix to choose depends on the implementation. ECMAScript 5 clarifies that 10 (decimal) should be used, but not all browsers support it. Therefore, it is important to specify a radix when using parseInt. 3. If the input string begins with any other value, the radix is 10 (decimal).Copy the code

So that explains why our parseInt(10, 0) is 10.

Array. The prototype. The map method

The map method takes two arguments. The first is a callback function that is executed for each item in the array. The callback takes three arguments: the element being processed, the index being processed, and the current array. The second argument is this, which calls the callback function.

The basic usage of the array.prototype. map method is shown in the following code:

function doubleNum(n, i) {
  let doubleN = 2 * n
  console.log(The double of {I +1} element is${doubleN}`)
  return doubleN
}
let newArr = [1.2.3].map(doubleNum)
console.log(newArr)
/* The result is as follows: the double of {I +1} element is the double of {I +1} element 2 is the double of {I +1} element 4 is the double of 6 [2, 4, 6] */
Copy the code

If this doesn’t make sense, we can move the function around as follows:

let newArr = [1.2.3].map(function (n, i) {
  let doubleN = 2 * n
  console.log(The double of {I +1} element is${doubleN}`)
  return doubleN
})
Copy the code

The problem solving

Now we should know the result of this problem. It works like this:

  • parseInt('1', 0), directly in base 10, the result is 1;
  • parseInt('2', 1), passing in the non2 ~ 36Value of, resulting in NaN;
  • parseInt('3', 2)In base 2, only 1 and 0 can be parsed, so returnNaN.

Now that we know the answer to this question, let’s take a look at the following code snippet and see what happens:

['10'.'10'.'10'.'10'.'10'].map(parseInt);
Copy the code
  • First of all,parseInt('10', 0)The answer is 10, you don’t have to explain;
  • thenparseInt('10', 1)The result of phi is NaN, as we explained earlier;
  • parseInt('10', 2)The result is 2, because 10 is 2 when converted to binary;
  • parseInt('10', 3)The result is 3, 10 converted to ternary is 3;
  • parseInt('10', 4)And so on.

The end result is [10, NaN, 2, 3, 4].