4 Replies Latest reply on Jul 13, 2015 7:34 AM by ashh.allen

# How to do I perform a conditional "or" calculation in PDF?

I am trying to create a calculation that allows my customers to calculate their cost. (Sounds simple...but I am new to JavaScript so I am struggling to fix it)

Objective: I want the formula to calculate the customer's total price based on their membership status plus any add-ons. So the total will either reflect a "NonMember" price with add-ons OR a "Member" price with add-ons.

Booth prices:

NonMember = \$900

Member = \$750

Booth Corner= \$90

Booth Extensions= \$95

here is what I have:

(function () {

var v1 = ( +getField("Booth_NonMember").value * 900 )  + ( +getField("Booth_Corner").value * 90 )  + ( +getField("Booth_Extensions").value * 95 );

var v2 = ( +getField("Booth_Member").value * 750 )  + ( +getField("Booth_Corner").value * 90 )  + ( +getField("Booth_Extensions").value * 95 );

var v3 = ( v1 || v2 );

event.value = v3;

})();

The problem is, when I enter text in the field for the Member price and then add text in the add-on fields it negates the Member price all together.

So if I say: I want 1 "Member" Booth (\$750) with 1 "Booth Corner" (\$90) and 1 "Booth Extensions" (\$95) the formula tells me that the total is \$185 when it should be \$935.

am I missing something?

• ###### 1. Re: How to do I perform a conditional "or" calculation in PDF?

The problem is your v1 value will always evaluate to true in that OR test if either of the booth fields has a value, so v1 will always be chosen over v2. Try this instead:

(function () {

var nNonMember = +getField("Booth_NonMember").value;

var nMember = +getField("Booth_Member").value;

var v1 = ( nNonMember * 900 )  + ( +getField("Booth_Corner").value * 90 )  + ( +getField("Booth_Extensions").value * 95 );

var v2 = ( nMember * 750 )  + ( +getField("Booth_Corner").value * 90 )  + ( +getField("Booth_Extensions").value * 95 );

event.value = nNonMember > 0 ? v1 : v2;

})();

• ###### 2. Re: How to do I perform a conditional "or" calculation in PDF?

I had to fix a typo in the code, so use the corrected version of the script as seen via the forums, as opposed to anything you may have received by email.

• ###### 3. Re: How to do I perform a conditional "or" calculation in PDF?

Thank you. It worked!

• ###### 4. Re: How to do I perform a conditional "or" calculation in PDF?

What would my event.value equal if I have three var?