6 Replies Latest reply on Mar 15, 2015 2:48 AM by Bloodywog

# Custom calculation  - Multiple If Statements

Hi Guys

I'm stuck with a simple If Statement. New to JavaScript.

I have a "Age Field" text field, "Gender" Drop Down field and "Risk" text field.

In the "Risk" text field I need to have a Custom calculation script to find out if "Male" >= 45 or Female >= 55, +1 Risk.

I currently have using this:

event.value = "";

var a = +this.getField("Age").value

var b = +this.getField("Gender").value

if (a>=45 && b="Male") event.Value = "+1"

else if (a<=45 && b="Male") event.Value = "0"

if (a>=55 && b="Female") event.Value = "+1"

else if (a<=55 && b="Female") event.Value = "0"

Any help will be appreciated.

• ###### 1. Re: Custom calculation  - Multiple If Statements

The comparison operator is "==". So instead of this:

if (a>=45 && b="Male")

You need to use:

if (a>=45 && b=="Male")

• ###### 2. Re: Custom calculation  - Multiple If Statements

Hi try67

I have modified the code to use the correct comparison (==) but the Risk Field still is empty.

The current custom script in the Risk field is:

event.value = "";

var a = +this.getField("Age").value

var b = +this.getField("Gender").value

if (a>=45 && b=="Male") event.Value = "+1"

else if (a<=45 && b=="Male") event.Value = "0"

if (a>=55 && b=="Female") event.Value = "+1"

else if (a<=55 && b=="Female") event.Value = "0"

What am I missing?

• ###### 3. Re: Custom calculation  - Multiple If Statements

First of all, you need to add "else" to the start of the penultimate line.

Also, open the JS console (Ctrl+J) and make sure that there are no errors

there.

And keep in mind that the script will only "kick in" once you change the

value of some field.

• ###### 4. Re: Custom calculation  - Multiple If Statements

try67

Still doesn't make sense. Could you place the "else" in the line for me?

I understand the script only kick in when a change is made. No errors using JS Console.

• ###### 5. Re: Custom calculation  - Multiple If Statements

I'm noticing another error. You're attempting to convert the Gender value to a number with the plus sign, even though it's not. That produces an error and prevents your code from working.

Here's the fixed code:

```var a = +this.getField("Age").value;
var b = this.getField("Gender").value;

if (a>=45 && b=="Male") event.Value = "+1";
else if (a<=45 && b=="Male") event.Value = "0";
else if (a>=55 && b=="Female") event.Value = "+1";
else if (a<=55 && b=="Female") event.Value = "0";
else event.value = "";
```

• ###### 6. Re: Custom calculation  - Multiple If Statements

Thank's so much. Working Great!

Understand my errors now!