These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

calculating with else-if statements

407chelsie
Registered: Dec 8 2008
Posts: 68
Answered

I need to calculate a total using one of three variables.

My Example:
Membership A: $565
Membership B: Calculated Field [i]b_total[/i]
Membership C: Calculated Field [i]c_total[/i]
Donate to Cause 1: Entered Amount [i]don_1[/i]
Donate to Cause 2: Entered Amount [i]don_2[/i]
Total: Calculated Amount

Only one membership total can be used.

If b_total and c_total are "0", I need the total to be (565 + don_1 + don_2)

If there is a value greater than 0 in Membership B:
the total will be (b_total + don_1 + don_2)

If there is a value greater than 0 in Membership C:
the total will be (c_total + don_1 + don_2)

I tried:

var b_total = +getField("StaffBTotal").value;
var c_total = +getField("StaffCTotal").value;
var don_1 = +getField("IIARF").value;
var don_2 = +getField("IIAAF").value;
 
if (b_total !== 0) {
event.value = b_total + don_1 + don_2;
} 
else if (c_total !== 0) {
event.value = c_total + don_1 + don_2;
} 
else {
event.value = 565 +don_1 + don_2;
}

What did I do wrong?

My Product Information:
Acrobat Pro 8.1.3, Macintosh
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Can you explain what problem you're experiencing? What is is giving you and how is it incorrect? Have you checked the field calculation order to make sure it makes sense for your form?

George
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
It appears you are not testing for all of your conditions.

var b_total = this.getField("StaffBTotal").value;var c_total = this.getField("StaffCTotal").value;// sum the donationsvar don = Number(this.getField("IIARF").value) + Number(this.getField("IIAAF").value); // clear the event valueevent.value = ''; if (b_total != 0 & c_total == 0) { // only b membershipevent.value = +b_total + don;} else if (b_total == 0 & c_total != 0) { // only c membershipevent.value = +c_total + don;} else if((b_total + c_total) != 0) {  // must have both b & c membership valuesapp.alert("You can only have 1 Staff type membership at most"); } else { // neither b nor c membershipevent.value = 565 + Number(don);}

Of you could use:
var b_total = this.getField("StaffBTotal").value;var c_total = this.getField("StaffCTotal").value;// sum the donationsvar don = Number(this.getField("IIARF").value) + Number(this.getField("IIAAF").value);// select for expressions that are trueswitch (true) { case ((+b_total == 0) && (+c_total == 0)) :// no b and no c membershipevent.value = 565 + don;break; case ((+b_total != 0) && (+c_total == 0)) :// only b membershipevent.value = +b_total + don;break; case ((+b_total == 0) && (+c_total != 0)) :// only c membershipevent.value = +c_total + don;break; default:// must be an error in membershipevent.value = '';app.alert("You can only have 1 Staff type membership at most");break;} // end switch selection

George Kaiser

407chelsie
Registered: Dec 8 2008
Posts: 68
After checking my calculation order, reading through the two suggestions and applying them both (one at a time, of course) I still get an empty Total field. I have posted [url=http://idisk.mac.com:80//chelsie.hall/Public/081198.pdf]my form[/url] on my MobileMe server. Hopefully, you non-Mac-ers can get to it an see something I'm missing.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
The main problem with your original code is this line:

var don_2 = +getField("IIAAF").value;
It should be:

var don_2 = +getField("IAAAF").value;
So it was a simple typo. You should set up your JavaScript preferences so that the JavaScript console is shown on errors. Your original code should work with the following slight modifications:

var b_total = +getField("StaffBTotal").value;var c_total = +getField("StaffCTotal").value;var don_1 = +getField("IIARF").value;var don_2 = +getField("IAAAF").value; if (b_total !== 0 && c_total === 0) {event.value = b_total + don_1 + don_2;}else if (b_total === 0 && c_total !== 0) {event.value = c_total + don_1 + don_2;}else if (b_total > 0 && c_total > 0) {// You might want to do something else hereevent.value = "";}else {event.value = 565 + don_1 + don_2;}

If the user should not be filling in the B and C section simultaneously, you might consider preventing them from doing so, perhaps by adding check boxes to control which section is active.

George
407chelsie
Registered: Dec 8 2008
Posts: 68
I ended up using gkaiseril's second "case theory" and fixing the typo George pointed out. The calculation works great! But I wonder if it can be better.

Two problems:

(1) If the user fills out the 1-5 member number and a 6-100 member number, the "warning" doesn't come up. It only pops up if they attempt to enter the 6-100 and 101+ fields.

(2) The 565 total shows up even before the user enters a 1-5 number. Can this be blank until a number of members is entered?

I have updated the [url=http://idisk.mac.com:80//chelsie.hall/Public/081198.pdf]form here[/url].