A: the topic

  • Today aboutparseInt, have seen such a question before:
    ['1'.'2'.'3'].map(parseInt)
Copy the code
  • Output what? Some people might say: [1, 2, 3], which is actually the wrong answer.
  • Let’s see what the correct answer is, in the console printout:

    His output was:[1, NaN, NaN]Some people are wondering why does it output[1, NaN, NaN]Rather than[1, 2, 3].

Two: re-examine the topic

    ['1'.'2'.'3'].map(parseInt)
Copy the code
  1. map(parseInt)You might ask a lot of questions, what is this? This is actually shorthand. Let’s look at other examples
    • So this is the shorthand['1', '2', '3'].map(parseInt)
    • The full version is as follows['1', '2', '3'].map((v, i, arr) => parseInt(v, i))
  2. map()This is a callback function with three argumentsThe current value (v).The subscript (I).Raw Array (ARR)
  3. Order of execution:['1', '2', '3'].map((v, i, arr) => parseInt(v, i)), output in turn:
    • parseInt('1', 0) // 1
    • parseInt('2', 1) // NaN
    • parseInt('3', 2) // NaN

Three: parseInt

Now, some of you might be wondering why the first output is 1, and the second and third output is NaN, which brings us to the topic of today, right

  1. What is the parseInt?

    MDN: parseInt(String, radix) converts a string string to an integer with radix between 2 and 36. All are returned in decimal form.

    Address: developer.mozilla.org/zh-CN/docs/…

    So the second argument to parseInt is base, but it also says anything between 2 and 36 in base

  2. Remove doubt

    • So according toparseIntThe second parameter of the base range, we can knowparseInt('2', 1)Why is equal toNaN
    • So whyparseInt('1', 0)Is equal to the1? From MDN aboutparseIntThe screenshots

      So 0 is going to be counted as base 10, soparseInt('1', 0)Is equal to the1

    • So whyparseInt('3', 2)Is equal to theNaN? Before we answer that question, let’s look at the base system

    So parseInt’s first parameter string is less than the second parameter radix, so parseInt(‘3’, 2) equals NaN;

    • Based on the above inference, let’s add more to the list.

      An array of'3',... '9'And the subscript of that is theta2 ~ 8The first parameter is greater than the base, so bothNaN

    • So why does parseInt(’10’, 9) print 9 instead of NaN when the first argument is ’10’ in base 9?

      • According to the previous description of base,9The real numbers in base form are0 ~ 8In between. The first argument is whatever is passed instringornumber, it will be parsed into strings one by one, so in9In the system'1'and'0'Belong to0 ~ 8The real number
  3. operation

    1. How can I do it without the console?

      Before answering that question take a look at the screenshot from MDN:

      ParseInt (‘123’, 5); parseInt(‘123’, 5); parseInt(‘123’, 5); parseInt(‘123’, 5); 1*Math.pow(5, 2) + 2*Math.pow(5, 1) + 3*Math.pow(5, 0)

      • Let’s see how it works out, okay

        He will make'123'Parse string by string'1'.'2'.'3'And then multiply by each of themMath.pow(x, y);

        X: is the base, the base here is the second parameter of 'parseInt' radix 'Y: is the power, the power here is the "right to left" subscript of the first parameter of' parseInt 'Copy the code
      • Let’s try a few more

        • parseInt('4123', 5); / / 538
        • 4*Math.pow(5, 3) + 1*Math.pow(5, 2) + 2*Math.pow(5, 1) + 3*Math.pow(5, 0) // 538

        • parseInt('764', 8); / / 500
        • 7*Math.pow(8, 2) + 6*Math.pow(8, 1) + 4*Math.pow(8, 0); / / 500
  4. extension

    1. ParseInt’s first argument to string must have a real number less than the base number. Okay?

      Let’s take a look at the screenshot:

      • As shown in figure:
        • The first two work. Why the thirdparseInt('125', 5)The output is7, this does not conform to the above calculation formula, and it was later found that it automatically put the final5I deleted it. The output is actuallyparseInt('12', 5)So the output is equal to7.
        • Why is the fourth outputNaN, because the parameterstringhas5At the beginning, does not match the base number, so returns directlyNaN.
        • The last one goes straight to55Deleted, finally resolved toparseInt('1', 5)And the output1
    2. Conclusion:

      • If the first argumentstringThe first value of is greater than or equal to the second argumentradix, then return directlyNaN
      • If the first argumentstringThe first value of, and the other values do not meet the conditions, then the values that do not meet the conditions will be ignored in the end, and then the calculation

Four: extracurricular exercises

1. ['10'.'10'.'10'.'10'].map(parseInt); 1*5^2 + 2*5^1 + 3*5^0 // what is output, do you know how it works?Copy the code

Write a blog for the first time, if have flaw, still ask everybody big guy much forgive