5 Replies Latest reply on Feb 3, 2014 10:30 AM by meljar

# Conditional If/Else statement

I have a text field that calculates a numerical value (AUDITC Total) and a group of Radio buttons for Gender (Male, Female). I want the following conditions to apply:

1. If the person is male and their AUDITC Total score is greater than or equal to 4, I want a "Yes" to entered into another text field (Screen). If the AUDIT score is less than 4, I want a "No" to be entered into the Screen textfield.

2. If the person is female and their AUDITC Total score is greater than or equal to 3, I want a "Yes" to be entered into the Screen text field and if the AUDIT score is less than 3 I want a "No" to be entered into the Screen textfield.

I have entered the following code into the custom calculation area of the Screen text field box and it worked initially. I clicked Male and had a 3 as the AUDIT score, the Screen textfield came up with a "No". However, when I changed the value from Male to Female (AUDIT score was still 3), the Screen text field didn't change from No to Yes. I think I'm on the right track, but maybe there is a better way to do this, or something else is wrong)?

var v = this.getField("AUDITC Total").value;

var g = this.getField("gender 1").value;

if (v=="Off" && g=="Off") event.value = "";

else if (g=="Male" && v<"4") event.value = "No";

else if (g=="Male" && v=>"4") event.value = "Yes";

else if (g=="Female" && v<"3") event.value = "No";

else if (g=="Female" && v=>"3") event.value ="Yes";

• ###### 1. Re: Conditional If/Else statement

If you put something in (double) quotes it becomes a string and can't be used for mathematical comparisons.

So replace this:

v<"4"

With this:
v<4

Also, there's no such operator as "=>". It should be ">=".

• ###### 2. Re: Conditional If/Else statement

Hi,

Thanks for the quick reply. It still doesn't seem to work properly. I took out the double quotes and changed the operator. So, now it looks like this:

var v = this.getField("AUDITC Total").value;

var g = this.getField("gender 1").value;

if (v=="Off" && g=="Off") event.value = "";

else if (g=="Male" && v<4) event.value = "No";

else if (g=="Male" && v>=4) event.value = "Yes";

else if (g=="Female" && v<3) event.value = "No";

else if (g=="Female" && v>=3) event.value ="Yes";

However the Screen textfield still doesn't consistently update to the correct value (No or Yes) if I change the gender or AUDIT score values.

• ###### 3. Re: Conditional If/Else statement

Does anyone have an idea as to why the value in my Screen textfield won't update if the value is changed in either the AUDITC Total field or the gender 1 field? I'm really getting frustrated trying to figure it out.

• ###### 4. Re: Conditional If/Else statement

If the values are updating after the next change, check your calculation order for the fields. This is not the same as the tab order.

• ###### 5. Re: Conditional If/Else statement

Thanks!! It works now!