It is the bitwise AND assignment.
In your code example, let's take a look at the first one:
allRatioAreGood &= (arr[i]/arr[i-1] == ratio);
the values arr[i]
and arr[i-1]
are divided by each other (producing a ratio), and that ratio is tested for equality (using ==
), producing a true
or false
value.
That value is then bitwise AND-ed to the existing value of allRatioAreGood
, which means that if the previous value for allRatioAreGood
was false (binary 0
), then all future values for allRatioAreGood
would also result in false (because of the AND operation, 0 & 1 = 0).
The result of this is that allRatioAreGood
and allDiffAreGood
figures out if all of the values in that array pass that test for equality, that is, if all the values have that "good ratio" or "good difference".
However, in this particular example, the comparator is checking for equality between a ratio and the result of a division, meaning that because the result of a division operation is a floating point number, no other number would be equal to it when testing with ==
. To test for "close enough equals", an epsilon needs to be used. See this other SO answer for more info: What's wrong with using == to compare floats in Java? (don't worry that this is Java vs. JavaScript, the concept is the same). Additionally, this gets even more complicated because in Javascript, if the result of ratio
or the division operation is NaN
or Infinity
, you will always get false
when comparing the two numbers.
From the docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators
Bitwise AND assignment
The bitwise AND assignment operator uses the binary representation of both operands, does a bitwise AND operation on them and assigns the result to the variable. See the bitwise AND operator for more details.
Syntax
Operator: x &= y
Meaning: x = x & y
Example
var bar = 5;
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
bar &= 2; // 0