1
function ArithGeo(arr) {
  var diff = arr[1]-arr[0];
  var ratio = arr[1]/arr[0];
  var allRatioAreGood = true;
  var allDiffAreGood = true;
  for(var i = 2;i<arr.length;i++){
  //  console.log(arr[i]-arr[i-1] == diff);
    allRatioAreGood &= (arr[i]/arr[i-1] == ratio);
    allDiffAreGood &= (arr[i]-arr[i-1] == diff);
  }

  // code goes here
  if (allRatioAreGood){
    return "Geometric"
  }else if (allDiffAreGood){
      return "Arithmetic";
    }else {
    return -1
  }

}
console.log(ArithGeo([2, 4, 6, 7 , 8 ,10]));

what does '&=' mean when we used it in the for loop? and how does it work? can someone explain please?

also, how does those two lines work?

allRatioAreGood &= (arr[i]/arr[i-1] == ratio);
allDiffAreGood &= (arr[i]-arr[i-1] == diff);  

Can anyone explain please ?

Barmar
  • 596,455
  • 48
  • 393
  • 495

4 Answers4

5

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
Community
  • 1
  • 1
Kyle Falconer
  • 7,586
  • 5
  • 44
  • 63
1

a &= b;

is the same as saying:

a = a & b;

So why use this form? Maybe you have a more complex expression, and want to make is clear that a and a are the same lvalue:

obj.array[index].bits = obj.array[index].bits & mask;

versus:

obj.array[index].bits &= mask;

Jim Cote
  • 1,716
  • 3
  • 15
  • 26
0

Its a bitwise operator:

result = expression1 & expression2

a &= b; is equivalent to a = a & b;

MDN Docs on bitwise operators

omarjmh
  • 11,814
  • 6
  • 29
  • 40
0

Bitwise AND assignment, pretty much if you have 5 & 3, the use of bitwise converts the numbers to base 2, which is 00101 & 00011 (many more 0's in front) and get 1 because the one's digit's are both 1 (true)

Generally, && is a multiplication, where || is addition, same goes for bitwise, 1 & 1 is 1 * 1 and 1 | 1 is 1 + 1 which is truthy (even though it's still considered a 1..)

neaumusic
  • 8,193
  • 7
  • 41
  • 65