Big factory is hanging, this interview question completely confused me

Welcome to pay attention to my public account “Life Code”

First interview questions every day

The title is as follows:

['1'.'2'.'3'].map(parseInt)
Copy the code

And then you say what the return value is, and why?

Have to say, dachang is hanging ah, is dachang really can make rockets, or our foundation is not very good, obviously the second kind? The average person would answer [‘1’, ‘2’, ‘3’].

They don’t answer [‘1’, ‘2’, ‘3’], but [1, NaN, NaN].

That’s why I’m at a loss. That’s why I’m hard to get. Oh, that’s why I’m hard to get.

So let’s take a look at why?

What is the interviewer really trying to ask?

I guess there are several possibilities

  • The map method
  • ParseInt method

Map method parsing

['1'.'2'.'3'].map(function(item){
  console.log(item)
})
Copy the code

You’d think he’d just print 1,2,3. No, let’s see how the browser console prints:

1
2
3
(3) [undefined.undefined.undefined]
Copy the code

Function (item){} returns undefined by default. Function (item){} returns undefined by default

So map then returns an array of three undefined elements

[undefined.undefined.undefined]
Copy the code

Actually, we could write it up here, just to make it a little bit clearer

var a = ['1'.'2'.'3'].map((item) = > console.log(item))
console.log(a)
Copy the code

In fact, print out:

[undefined.undefined.undefined]
Copy the code

Further research:

['1'.'2'.'3'].map(function(item, index){
  // item: array element
  // index: the index of an array element
  console.log(item, index)
})
Copy the code

Will print:

item, index
1      0
2      1
3      2
Copy the code

Further research:

['1'.'2'.'3'].map(function(item, index, array){
  console.log("item", item)
  console.log("index", index)
  console.log("array", array)
})
Copy the code

Will print:

item 1
index 0
array (3) ["1"."2"."3"]
item 2
index 1
array (3) ["1"."2"."3"]
item 3
index 2
array (3) ["1"."2"."3"]
Copy the code

Here’s a key difference between Map and forEach:

1.Map returns the array again2.ForEach does not return the arrayCopy the code

ok

parseInt

The key point to remember is that parseInt(String, radix) parses a string and returns decimal integers of the specified radix. Radix is integers between 2 and 36, representing the radix of the parsed string.

ParseInt (‘1’, ‘2’, ‘3’); parseInt (‘1’, ‘2’, ‘3’);

In fact, the above question can be translated into:

['1'.'2'.'3'].map(parseInt(item, index))
Copy the code

Is that right? Yeah, exactly, he’s going to take the array element as the first argument to parseInt, and the index of the array element as the second argument to parseInt

So why [1, NaNa, NaN]?

This is executed three times:

parseInt("1".0) / / 0
parseInt('2'.1) // NaN
parseInt('3'.2) // NaN
Copy the code

The first element is 1.

If the radix is undefined, 0, or unspecified, JavaScript assumes the following:

  • 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.

  • 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.

  • If the input string begins with any other value, the radix is 10 (decimal).

  • If the first character cannot be converted to a number, parseInt returns NaN.

So the second element is NaN, which I’m curious about.

Think about it:

Radix optional

The number from 2 to 36 represents the cardinality of the string. For example, if 16 is specified, the parsed value is a hexadecimal number. Note that 10 is not the default!

There’s no base 1 conversion, so NaN.

Why is the third element NaN?

parseInt('3'.2) // NaN
Copy the code

Because base 2 conversion, only base 0 and 1 and 2 can’t represent 3

So it’s NaN printed out