5 Replies Latest reply on Aug 1, 2014 4:00 PM by gkaiseril

# Problems with Multiple Conditions

So I have been able to create what I need but it does not work the way it should. So Im missing something. In the Attached form Im trying to provide a outcome with two to three different conditions. Based on Construction and Square footage I get a Heat Factor. (See table in pdf) But For some reason I can only get it to work half the time and usually it will only display Tight construction from the drop down even though another type of construction is selected.

Construction drop down values:

Tight =1

Average=2

Loose=3

Heat Factor field Calc script:

```event.value = 0;
var nSquareF = this.getField("F72").value;
var nCon = this.getField("D3").value;
if ( (nCon=1) && (nSquareF < 900) ) event.value = 0.41;
else if ( (nCon=1) && (nSquareF >= 901) && (nSquareF <= 1500) ) event.value = 0.31;
else if ( (nCon=1) && (nSquareF >= 1501) && (nSquareF <= 2000) ) event.value = 0.26;
else if ( (nCon=1) && (nSquareF >= 2001) && (nSquareF <= 3000) ) event.value = 0.22;
else if ( (nCon=1) && (nSquareF > 3001) ) event.value = 0.19;

else if ( (nCon=2) && (nSquareF < 900) ) event.value = 0.61;
else if ( (nCon=2) && (nSquareF >= 901) && (nSquareF <= 1500) ) event.value = 0.45;
else if ( (nCon=2) && (nSquareF >= 1501) && (nSquareF <= 2000) ) event.value = 0.38;
else if ( (nCon=2) && (nSquareF >= 2001) && (nSquareF <= 3000) ) event.value = 0.32;
else if ( (nCon=2) && (nSquareF > 3001) ) event.value = 0.28;

else if ( (nCon=3) && (nSquareF < 900) ) event.value = 1.29;
else if ( (nCon=3) && (nSquareF >= 901) && (nSquareF <= 1500) ) event.value = 0.94;
else if ( (nCon=3) && (nSquareF >= 1501) && (nSquareF <= 2000) ) event.value = 0.8;
else if ( (nCon=3) && (nSquareF >= 2001) && (nSquareF <= 3000) ) event.value = 0.66;
else if ( (nCon=3) && (nSquareF > 3001) ) event.value = 0.58;
```

• ###### 1. Re: Problems with Multiple Conditions

The comparison operator in JS is "==", not "="... That's the assignment operator.

• ###### 2. Re: Problems with Multiple Conditions

Awesome- Thank you so much. That is exactly what I was missing.

• ###### 3. Re: Problems with Multiple Conditions

For complex selections I would use the "switch" statement and use the logical statement for expression for the case statement.

var nSquareF = this.getField("F72").value;
var nCon = this.getField("D3").value;
switch(true) {
case( (nCon=1) && (nSquareF < 900) ):
event.value = 0.41;
break
case ( (nCon=1) && (nSquareF >= 901) && (nSquareF <= 1500) ) :
event.value = 0.31;
break;
case( (nCon=1) && (nSquareF >= 1501) && (nSquareF <= 2000) ) :
event.value = 0.26;
break;
case ( (nCon=1) && (nSquareF >= 2001) && (nSquareF <= 3000) ) :
event.value = 0.22;
break;
case ( (nCon=1) && (nSquareF > 3001) ) :
event.value = 0.19;
break;

case ( (nCon=2) && (nSquareF < 900) ) :
event.value = 0.61;
break;
case ( (nCon=2) && (nSquareF >= 901) && (nSquareF <= 1500) ) :
event.value = 0.45;
break;
case ( (nCon=2) && (nSquareF >= 1501) && (nSquareF <= 2000) ) :
event.value = 0.38;
break;
case ( (nCon=2) && (nSquareF >= 2001) && (nSquareF <= 3000) ) :
event.value = 0.32;
break;
case ( (nCon=2) && (nSquareF > 3001) ) :
event.value = 0.28;
break;
case ( (nCon=3) && (nSquareF < 900) ) :
event.value = 1.29;
case ( (nCon=3) && (nSquareF >= 901) && (nSquareF <= 1500) ) :
event.value = 0.94;
break;
case ( (nCon=3) && (nSquareF >= 1501) && (nSquareF <= 2000) ) :
event.value = 0.8;
break;
case ( (nCon=3) && (nSquareF >= 2001) && (nSquareF <= 3000) ) :
event.value = 0.66;
break;
case ( (nCon=3) && (nSquareF > 3001) ) :
event.value = 0.58;
break;
default:
event.value = "";
break;
}

• ###### 4. Re: Re: Problems with Multiple Conditions

You're still using the wrong operator in your code, though...

I would have do it differently. Something like this, a nested if-condition:

```if (nCon==1) {
if ((nSquareF >= 901) && (nSquareF <= 1500)) event.value = 0.31;
else if ((nSquareF >= 1501) && (nSquareF <= 2000)) event.value = 0.26;
else if ((nSquareF >= 2001) && (nSquareF <= 3000)) event.value = 0.22;
else if (nSquareF > 3001) event.value = 0.19;
} else if (nCon==2) {
// etc.
} else if (nCon==3) {
// etc.
}
```
• ###### 5. Re: Problems with Multiple Conditions

Corrected code:

var nSquareF = this.getField("F72").value;
var nCon = this.getField("D3").value;
switch(true) {

defaut:

// none of the following matched;

event.value =0; break;
case( (nCon == 1) && (nSquareF < 900) ):  event.value = 0.41;  break
case ( (nCon == 1) && (nSquareF >= 901) && (nSquareF <= 1500) ) : event.value = 0.31;  break;
case( (nCon == 1) && (nSquareF >= 1501) && (nSquareF <= 2000) ) :  event.value = 0.26;  break;
case ( (nCon == 1) && (nSquareF >= 2001) && (nSquareF <= 3000) ) :  event.value = 0.22;  break;
case ( (nCon == 1) && (nSquareF > 3001) ) : event.value = 0.19;  break;

case ( (nCon == 2) && (nSquareF < 900) ) :  event.value = 0.61;   break;
case ( (nCon == 2) && (nSquareF >= 901) && (nSquareF <= 1500) ) :  event.value = 0.45;  break;
case ( (nCon == 2) && (nSquareF >= 1501) && (nSquareF <= 2000) ) :  event.value = 0.38;  break;
case ( (nCon == 2) && (nSquareF >= 2001) && (nSquareF <= 3000) ) : event.value = 0.32;  break;
case ( (nCon == 2) && (nSquareF > 3001) ) :  event.value = 0.28;   break;
case ( (nCon == 3) && (nSquareF < 900) ) :  event.value = 1.29;  break;

case ( (nCon == 3) && (nSquareF >= 901) && (nSquareF <= 1500) ) : event.value = 0.94;  break;
case ( (nCon=3) && (nSquareF >= 1501) && (nSquareF <= 2000) ) : event.value = 0.8;  break;
case ( (nCon == 3) && (nSquareF >= 2001) && (nSquareF <= 3000) ) : event.value = 0.66;  break;
case ( (nCon == 3) && (nSquareF > 3001) ) : event.value = 0.58;  break;
} // end of switch;