Title 1
Let arr = [” 0x1 “, “0x2”, “0x3”]. Map (parseInt), what is the output of arr?
The results of
Correct result: [1,NaN,0]
parsing
First the parseInt method: parseInt(string, radix) parses a string and returns a decimal integer of the specified radix. Radix is an integer between 2-36, representing the radix of the parsed string.
The radix parameter is optional, if the radix is not selected or equal to 0, the method will first identify the string number as a decimal number, and then convert it to a decimal number.
- Because parseInt can pass two arguments, it automatically passes item and index arguments in order.
- So arR is equal to:
arr = [parseInt("0x1", 0), parseInt("0x2", 1), parseInt("0x3", 2)]
;
1.The first element: radix equals0, soparseIntIt will automatically recognize if string is a correct base number and find"0x1"Is a16Base numbers, so it will take0x1"As a16The base number is then converted to10Into the system.2.The second element: radix =1Because there is no non-existence1Base number, so it just returnsNaN.3.The third element: radix =2."0x3"As a binary number and then converted to10Base numbers, binary numbers only contain0and1, so in"0x3In the first place to meet the condition is only0So two mechanisms0into10Into the system for0.So the output is [1.NaN.0].
Copy the code
Topic 2
Let arr = [1,2,3]. Map (parseInt)
The results of
[1,NaN,NaN]
parsing
- Arr is equivalent to:
arr = [parseInt("1", 0), parseInt("2", 1), parseInt("3", 2)]
1.The first element: radix equals0, so identify which base string is in and convert it to decimal2.The second element: radix equals1There is no1Base number, so returnNaN
3.The third element: radix equals2Only exists in binary0or1.3Does not exist, therefore returnsNaN
Copy the code