0

So I have this long if statement.

if(dupeCheck[0].length > 1 || dupeCheck[1].length > 3 || dupeCheck[2].length > 1 || dupeCheck[3].length > 1){
    alert('Copy!');
}

It is possible to group together the dupeCheck[0].length > 1 parts? Like:

if((dupeCheck[0].length||dupeCheck[2].length||dupeCheck[3].length) > 1 || dupeCheck[1].length > 3){
    alert('Copy!');
}

Or:

if(dupeCheck[0].length||dupeCheck[2].length||dupeCheck[3].length > 1 || dupeCheck[1].length > 3){
    alert('Copy!');
}

I tried both, neither of them worked.

Marian
  • 191
  • 1
  • 4
  • 15
  • possible duplicate of [How to check multiple elements in array with one if with JavaScript?](http://stackoverflow.com/questions/4067688/how-to-check-multiple-elements-in-array-with-one-if-with-javascript) – isherwood Mar 12 '14 at 23:47
  • Or maybe http://stackoverflow.com/questions/15382722/compare-one-value-with-each-value-of-array-which-is-inside-another-array – isherwood Mar 12 '14 at 23:49
  • I'm not sure. Inside the arrays there are `.getElementsByClassName`, so I'm checking if inside each string there are multiple found elements and execute a command, but one of the array value already has 3 of the same class. – Marian Mar 12 '14 at 23:51
  • @isherwood Thanks for the links, but I'm not sure those questions exactly answer the OPs question. – Peter Mar 13 '14 at 00:08
  • @Marian it sounds like you have a bigger problem you are trying to solve. If you can provide some details on that (possibly in another question) you may find we can provide a better overall solution. – scunliffe Mar 13 '14 at 00:25
  • It's just that in the array I have `.getElementsByClassNames`. And basically with the if statement I'm detecting if inside each string the element with the same class is duplicated. So three of the classes must remain unique while the last class is used by three element. If the statement detects otherwise it gives an `alter();` – Marian Mar 13 '14 at 12:30

2 Answers2

1

Depending on how many tests you need to run you could also try something like this.

function isGreater(minimum){
  for(var i=1;i<arguments.length;i++){
    if(arguments[i] > minimum){
      return true;
    }
  }
  return false;
}

if(isGreater(1, dupeCheck[0].length, dupeCheck[2].length, dupeCheck[3].length) || isGreater(3, dupeCheck[1].length)){
  alert('Copy!');
}

This will let you test any number of values against a number e.g.

if(isGreater(5, 0,0,0,0,2,1,2,3,4) || isGreater(7, 1,3,1,2,1,9,2,1)){
  alert('Copy!');
}

Since JavaScript will let you pass as many parameters as you want to a function the 2nd to Nth parameters are tested against the 1st parameter "minimum" and as soon as one passes the test the function returns true. You could adjust the logic to test for other conditions or even ensure that all parameters pass the condition not just one.

scunliffe
  • 62,582
  • 25
  • 126
  • 161
0

There is no built-in language construct to do that.

But what you could do is something like this:

if(Math.min(dupeCheck[0].length, dupeCheck[2].length, dupeCheck[3].length) > 1 || dupeCheck[1].length > 3) {
    alert('Copy!');
}
Peter
  • 3,998
  • 24
  • 33
David
  • 148
  • 6