A front-end interview question
['1', '2', '3'].map(parseInt)
Fantastic results:[1, NaN, NaN]
Why is this happening? ? ? ? ?
- First, the map function syntax:
var new_array = arr.map(function callback(currentValue[, index[, array]]) { // Return element for new_array }[, thisArg])
The first element represents the element to be processed, and the second parameter represents the index of the element.
- Second, the parseInt syntax
parseInt(string, radix) parses a string and returns a decimal integer in the specified radix. radix is an integer between 2-36, indicating the radix of the parsed string.
Its syntax is:parseInt(string, radix)
. string represents the value to be parsed, radix is an optional parameter, with values from 2 to 36, indicating the base of the string. For example, specifying 16 indicates that the parsed value is a hexadecimal number. Please note that 10 is not the default value!
-
Therefore, the order of execution of the questions is:
parseInt('1', 0)
//When radix is 0 and the string parameter does not start with "0x" or "0", it will be processed based on 10. return 1parseInt('2', 1)
//The base is 1 (base 1), the maximum value is less than 2, cannot be parsed, and returns NaNparseInt('3', 2)
//The base is 2 (binary), and the binary values are only 0 and 1, which cannot be parsed and return NaN.
-
Result: Since the map function returns an array, it eventually returns
[1, NaN, NaN]
What about testing other possible arrays? ?
[0,2,1].map(parseInt)
=> [0, NaN, 1]
[10,3,2,4,5,6,7,8].map(parseInt)
=> [10, NaN, NaN, NaN, NaN, NaN, NaN, NaN]
[1110,30,0,2,3,6,7,8].map(parseInt)
=> [1110, NaN, 0, 2, 3, NaN, NaN, NaN]
[0,2,1,2,3,4,5,6,8,8,9,10,13].map(parseInt)
=> [0, NaN, 1, 2, 3, 4, 5, 6, NaN, 8, 9, 11, 15]
[0,2,1,2,3,4,5,6,7,8,9,10,13].map(parseInt)
=> [0, NaN, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 15]
As can be seen from the above, the parsing results of the same value in different array positions will be different (but careful identification and analysis may be required). A summary of the parsing rules of parseInt can be listed:
Conclusion: The rules of parseInt parsing with different bases
- The base is 0: After the string is converted to decimal, the result is 0 no matter what the value is;
- The base is 1: After converting string to decimal, the result is NaN regardless of the value;
- The base is 2: After the string is converted to decimal, except for 0 and 1, the rest of the results are NaN (binary only has two numbers: 0 and 1);
- The base is between 3-9: After the string is converted to decimal, if it is less than the base (greater than 0), the result is the number converted to decimal; if it is greater than the base after conversion and less than 10, the result is NaN; if it is greater than or equal to 10 , converted according to the rules;
- Base 10: The value itself after string is converted to decimal;
- The base is greater than or equal to 11: Convert according to the rules between different base values, for example:
parseInt(11,5) = 1*5^0 + 1 * 5^1 = 6
parseInt(101,25) = 1*25^0 + 0*25^1 + 1 * 25^2 = 626
that's all.
Best wishes: Come ashore soon!