Supernovah
Supernovah

Reputation: 2045

And Or stacking conditions in Javascript (if $1 && ($2 || $3))

For example. there are 3 variables, 1 is a must, 2 and 3 are eithers so 3 can be false as long as 1 and 2 are true, 2 can be false as long as 1 and 3 are true.

if(xmlhttp.responseText.indexOf("type:SearchList~")>=0 && (obj == "hDrop" || obj == "iDrop")){
}

Isn't working for me

Can anyone spot the problem?

Upvotes: 0

Views: 1175

Answers (3)

nivhab
nivhab

Reputation: 687

No problem with your script that I can spot. This very simple test validates it:

var a = "yes";
var b = "no2";
var c = true;
alert(c && (a == "yes" || b == 'no'));

Check the values of 'obj'. Is it a string?

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798646

I built a truth table of your conditions:

1 2 3 R
-------
0 X X 0
1 0 0 0
1 1 X 1
1 X 1 1

This resolves to 1 && (2 || 3), so something else is wrong.

Upvotes: 4

Ahmy
Ahmy

Reputation: 5480

You code is written correctly but it may be there are error in data you compare with so please trace the data and then check that it execute the condition correctly you can trace using alert(data);

Upvotes: 1

Related Questions