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();
}
&&
to a ||
is the solution
– Satpal
Commented
Oct 31, 2017 at 10:11
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.