I entered the following code in the console today,
Huh? ! Why does it return 1? Isn't this the number 13 is converted to binary and returned?
This aroused my interest, and then I entered the following code
. Why? !
After reading the information, I learned
- After parseInt receives the parameters, it will parse the first parameter
- For example, if the first parameter is 345, then divide this number into 3 and 4 and 5.
- Take 3, 4 and 5 to compare with parameter 2.
- Parameter 2 is a hexadecimal number, to see if each digit of parameter 1 is within the maximum value of parameter 2
- For example, the maximum value of binary system is 1, the maximum value of binary system is 2, and so on. The maximum value of parameter 2 is 36
- If you find that any digit is not within the limit of parameter 2, then don’t look down, just take the first few digits that meet the rules for calculation
Calculation rules
Let ’s go directly to the example
first.
-
Split 46 into 4 and 6
-
Check if 4 is within the maximum value of hexadecimal (6)
-
Check whether 6 is within the maximum value of hexadecimal 7
-
Calculation
-
Returns 34
2nd
- Split 99 into 9 and 9
- Check if 9 is within the maximum value (4)
- There is no number in front of 9 and cannot be calculated
- Return NaN
3rd
-
Split 5 and 9 into 5 and 9
-
Check whether 5 is within the maximum value of hexadecimal (5)
-
Check whether 9 is within the maximum value of hexadecimal
-
9 is not there, just take the number 5 for calculation
-
Calculation
-
Returns 5
4th
- Split 2345 into 2, 3, 4, 5
- Check if 2 is within the maximum value of hexadecimal (6)
- Check whether 3 is within the maximum value of hexadecimal 7
- Check if 4 is within the maximum value of hexadecimal 7
- Check whether 5 is within the maximum value of hexadecimal
- start calculating
- Return result