admin管理员组

文章数量:1026406

Can anyone tell me what's wrong with this if statement?

If I use either of the two main conditions on their own the statement works fine but when I add that middle && statement, it stops working. I've searched online and can't see what's wrong.

P.S. I can even change that middle && to a || statement and it works as well. I'm so confused.

if ((containerId.id == "LineOne" && dropLoc == "dropLocation1.1") && (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"))
        {
            alert("finished");
            cdpause();      
        }

Can anyone tell me what's wrong with this if statement?

If I use either of the two main conditions on their own the statement works fine but when I add that middle && statement, it stops working. I've searched online and can't see what's wrong.

P.S. I can even change that middle && to a || statement and it works as well. I'm so confused.

if ((containerId.id == "LineOne" && dropLoc == "dropLocation1.1") && (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"))
        {
            alert("finished");
            cdpause();      
        }
Share Improve this question edited Oct 31, 2017 at 10:41 Zac 1,3033 gold badges17 silver badges28 bronze badges asked Oct 31, 2017 at 10:10 TheWee SealTheWee Seal 191 gold badge1 silver badge2 bronze badges 3
  • changing that middle && to a || is the solution – Satpal Commented Oct 31, 2017 at 10:11
  • 2 AND means AND. containerId.id cannot be "LineOne" and "LineTwo" at the same time. – Joel Commented Oct 31, 2017 at 10:14
  • I fifgured it out... My main issue is i'm an idiot... but besides that I have the same variables containerId, and dropLoc being used twice... I'm trying to use an array now... thanks – TheWee Seal Commented Oct 31, 2017 at 10:49
Add a ment  | 

4 Answers 4

Reset to default 4

I've searched online and can't see what's wrong.

I can even change that middle && to a || statement and it works as well

Because containerId.id can't be LineOne and LineTwo at the same time.

Similarly, dropLoc can't have two values at the same time.

But it can have one of the two values, so replace && with ||.

   if ((containerId.id == "LineOne" && dropLoc == "dropLocation1.1") || 
       (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"))
   {
        alert("finished");
        cdpause();      
   }

You could binte the two checks with an OR, because, you can not have two different values at the same time.

Beside that, you need no brackets, because of the operator precedence of logical AND && over logical OR ||.

if (
    containerId.id == "LineOne" && dropLoc == "dropLocation1.1" ||
    containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"
) {
    alert("finished");
    cdpause();      
}

Like Satpal said

 if(containerId.id == "LineOne" && dropLoc == "dropLocation1.1") || (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2")

You already had the correct solution, you should have a || (or) instead of a && (and) in the middle.

It's basic boolean logic: you have two expressions with "and", and you want to execute your code if either of those expressions are true, so you join those expressions with "or".

"If the name is John and it's Monday OR if the name is Jane and it's Tuesday , then remind them to shop for groceries." => on Monday, it's John's turn to go shopping, on Tuesday, it is Jane's.

Can anyone tell me what's wrong with this if statement?

If I use either of the two main conditions on their own the statement works fine but when I add that middle && statement, it stops working. I've searched online and can't see what's wrong.

P.S. I can even change that middle && to a || statement and it works as well. I'm so confused.

if ((containerId.id == "LineOne" && dropLoc == "dropLocation1.1") && (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"))
        {
            alert("finished");
            cdpause();      
        }

Can anyone tell me what's wrong with this if statement?

If I use either of the two main conditions on their own the statement works fine but when I add that middle && statement, it stops working. I've searched online and can't see what's wrong.

P.S. I can even change that middle && to a || statement and it works as well. I'm so confused.

if ((containerId.id == "LineOne" && dropLoc == "dropLocation1.1") && (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"))
        {
            alert("finished");
            cdpause();      
        }
Share Improve this question edited Oct 31, 2017 at 10:41 Zac 1,3033 gold badges17 silver badges28 bronze badges asked Oct 31, 2017 at 10:10 TheWee SealTheWee Seal 191 gold badge1 silver badge2 bronze badges 3
  • changing that middle && to a || is the solution – Satpal Commented Oct 31, 2017 at 10:11
  • 2 AND means AND. containerId.id cannot be "LineOne" and "LineTwo" at the same time. – Joel Commented Oct 31, 2017 at 10:14
  • I fifgured it out... My main issue is i'm an idiot... but besides that I have the same variables containerId, and dropLoc being used twice... I'm trying to use an array now... thanks – TheWee Seal Commented Oct 31, 2017 at 10:49
Add a ment  | 

4 Answers 4

Reset to default 4

I've searched online and can't see what's wrong.

I can even change that middle && to a || statement and it works as well

Because containerId.id can't be LineOne and LineTwo at the same time.

Similarly, dropLoc can't have two values at the same time.

But it can have one of the two values, so replace && with ||.

   if ((containerId.id == "LineOne" && dropLoc == "dropLocation1.1") || 
       (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"))
   {
        alert("finished");
        cdpause();      
   }

You could binte the two checks with an OR, because, you can not have two different values at the same time.

Beside that, you need no brackets, because of the operator precedence of logical AND && over logical OR ||.

if (
    containerId.id == "LineOne" && dropLoc == "dropLocation1.1" ||
    containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"
) {
    alert("finished");
    cdpause();      
}

Like Satpal said

 if(containerId.id == "LineOne" && dropLoc == "dropLocation1.1") || (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2")

You already had the correct solution, you should have a || (or) instead of a && (and) in the middle.

It's basic boolean logic: you have two expressions with "and", and you want to execute your code if either of those expressions are true, so you join those expressions with "or".

"If the name is John and it's Monday OR if the name is Jane and it's Tuesday , then remind them to shop for groceries." => on Monday, it's John's turn to go shopping, on Tuesday, it is Jane's.

本文标签: Multiple conditions using an AND if StatementJavaScriptStack Overflow