0

If got a number of values which I want to compare (they all have to be the same). The function e(row,col) returns an attribute of an element in a grid.

Is it possible to write something like…

if(e(1,2)==e(2,2)==e(3,2)==e(4,2)){...}

…instead of …

if(e(1,2)==e(2,2)&&e(2,2)==e(3,2)&&e(3,2)==e(4,2)){…}

?

MoPaMo
  • 517
  • 6
  • 24

1 Answers1

0

I'd make an array of parameters to pass to the function, then get the result of the first call, then check that .every one of the other array pairs are equal to the first:

const params = [
  [1, 2],
  [2, 2],
  [3, 2],
  [4, 2],
];
const res = e(...params[0]);
if (params.slice(1).every(args => e(...args) === res)) {
  // do stuff
}

Or use a loop, if the indicies are this predictable:

const res = e(1, 2);
let allMatch = true;
for (let i = 2; i <= 4; i++) {
  if (e(1, i) !== res) {
    allMatch = false;
    break;
  }
}
if (allMatch) {
  // do stuff
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320