问题
I am trying to decipher a piece of JS code (while (obviously) my JS knowledge is null).
What is the % operator in the code below?
m[p1%2][q1] = 0.0;
for(j=q1+1; j <= q2; j++)
m[p1%2][j] = m[p1%2][j-1] + 1;
回答1:
% is remainder operator. It gives the remainder after division.
Quoting from MDN
The remainder operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend, not the divisor. It uses a built-in modulo function to produce the result, which is the integer remainder of dividing
var1byvar2— for example — var1 modulo var2.There is a proposal to get an actual modulo operator in a future version of ECMAScript, the difference being that the modulo operator result would take the sign of the divisor, not the dividend.
回答2:
% is modulo (or modulus?). It divides the first term by the second and returns the remainder. This can be useful for looping through a set of numbers and many other things.
Usage: x % y
Examples:
5 % 10; //5
10 % 5; //0
16 % 5; //1
回答3:
% is a Mathematical operator, it is used for Modulus. in simple 3 % 2=1 Modulus operator returns reminder.
来源:https://stackoverflow.com/questions/37005330/what-is-the-operator-in-javascript