0

So I m building one game which is drenching the field according to selected colour and I have 4 different conditions:

let condition1 = drenchX + 1 === mixedX && drenchY === mixedY && this.mixedFields[i].style.backgroundColor === this.service.clickedColor;
let condition2 = drenchX  === mixedX && drenchY + 1 === mixedY && this.mixedFields[i].style.backgroundColor === this.service.clickedColor;
let condition3 = drenchX - 1 === mixedX && drenchY === mixedY && this.mixedFields[i].style.backgroundColor === this.service.clickedColor;
let condition4 = drenchX === mixedX && drenchY - 1 === mixedY && this.mixedFields[i].style.backgroundColor === this.service.clickedColor;

I need to get all possible combinations of this conditions, all 4 of them can be true, or 3 or 2 or just one. This works fine but I think it is too long. My if statement looks like this:

if((condition1 && condition2 && condition3 && condition4) || (condition1 && condition2 && condition3) || (condition1 && condition2 && condition4) || (condition1 && condition3 && condition4) || (condition2 && condition3 && condition4) || (condition1 && condition2) || (condition1 && condition3) || (condition1 && condition4) || (condition2 && condition3) || (condition2 && condition4) || (condition3 && condition4) || condition1 || condition2 || condition3 || condition4) {
// some action
}

It all works perfectly but I m looking for a shorter solution and I m still a beginner in this. So if anyone know a better way to do this that would be great, my statement is just too long and it doesn't looks good at all. Btw, project is in Angular. Maybe to create some function for this?

2
  • 1
    Are there any combinations of your conditions that you don't want to match, or do you want to execute the statement if any of them are true? Commented Jun 11, 2019 at 22:07
  • If I'm not mistaken, since you're using ors and you have ...|| condition1 || condition2 || condition3 || condition4) at the end, isn't this just equivalent to use if( condition1 || condition2 || condition3 || condition4) Commented Jun 11, 2019 at 22:09

3 Answers 3

2

If a && b is true, then a || b is also true. That said it means your condition can just be

if (condition1 || condition2 || condition3 ||  condition4)
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand correctly, the if-statement is checking whether any of the conditions are met. Therefore, this should do the trick:

if(condition1 ||
    condition2 ||
    condition3 ||
    condition4)
 {
   //some action
 }

4 Comments

Yes thats right, but I need to get all possible combinations of 4 conditions, it can be all 4 of them correct or 3 or 2 or just one.
@AnđelkoLipotić what matters? It enters inside the condition if just one of the condition is true. So why do you ever check the combinations?
Because I m changing colour of specific element this.mixedFields[i].style.backgroundColor so if first condition is true it is not gonna check others so its only gonna change colour of first element not of rest which are true.
The combinations won't exactly matter since you already have all the conditions stored in memory. You could attempt another avenue by having 4 different if-statements, each changing the color slightly if the condition is met
0

Thanks everyone for advices, but I found a way to simplify it. I stored conditions into array and used for-in to loop thru all possible conditions.

let conditionalArray: boolean[] = [condition1, condition2, condition3, condition4];

for(let c in conditionalArray){
  if(conditionalArray[c]) {
       //....
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.