Operator --- remainder operation %

Modulo operation
javascript modulo operation is to divide the value of one expression by the value of another expression and return the remainder.
Taking modulus in js means taking the remainder.

a%b         //是求余数;
a/b         //是求商;
Math.abs(x) //是求x的绝对值;

12除以5=2,余数是2,即5*2+2=12, 所以12%5=2
7除以3=2,余数是1,即3*2+1=7,所以7%3=1

The function acts on two integers (positive integers, negative integers), and the result of the operation is the remainder after the division of the two numbers, and the result of the operation is an integer.

Regulation:

(1) The positive and negative sign of the operation result is consistent with the sign of the dividend;

(2). When the dividend is smaller than the divisor, the operation result is equal to the dividend.

Example 1: 8%3=2 (the dividend is a positive number)

let n = 8 % 3; //2

Example 2: -8%3=-2 (the dividend is a negative number)

let n = -8 % 3; //-2

Example 3: 8%-3 (the dividend is positive and the divisor is negative)

let n = 8 % -3; //2

Example 4: 3%8=3 (the dividend is less than the divisor)

let n = 3 % 8; //3

You can refer to this article again

Deep understanding of remainder/modulo operation

おすすめ

転載: blog.csdn.net/weixin_48927323/article/details/127632768