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

Another conditional execution

DavidCouper
Registered: Apr 2 2011
Posts: 7
Answered

Hi
 
Having driven myself slightly insane, I thought I'd ask for help.
 
I'm a complete novice (obviously) and have a form with a total field and a deductions field. The Total field is simply the sum of various other fields and I need to put calculated deductions in a separate field (Deductions).
 
I tried to do it many ways but this is the general idea:
 
var dedTotal = this.getField("dedTotal").value;
if (dedTotal >=60)
event.value = -45;
else if ((dedTotal >=40) && (dedTotal <60))
event.value = -30;
else if ((dedTotal >=20) && (dedTotal <40))
event.value = -15;
else
event.value = 0;
  
Any help to get this working is greatly appreciated.
 
I've also seen "event.value = 0" put at the top of the script with no "else" statement. Is one 'better' than the other and why?

My Product Information:
Acrobat Pro 8.2.6, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
What is not working, exactly?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

DavidCouper
Registered: Apr 2 2011
Posts: 7
Accepted Answer
When I looked at this after a longish break - to view your reply, I saw what was needed:

var dedTotal = this.getField("Total").value;
if (dedTotal >=60)
event.value = -45;
else if ((dedTotal >=40) && (dedTotal <60))
event.value = -30;
else if ((dedTotal >=20) && (dedTotal <40))
event.value = -15;
else
event.value = 0;

Should have seen it before - sorry and thanks.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You could also use a switch statements instead of the nestted 'if' statement:

var dedTotal = this.getField("Total").value;
switch(true) {
case (dedTotal >= 60):
event.value = -45;
break; // exit test
case (dedTotal >= 40):
event.value = -30;
break; //exit test
case (dedTotal >= 20):
event.value = -15;
break; // exit test
default:
event.value = 0;
break;
} // end switch

George Kaiser

DavidCouper
Registered: Apr 2 2011
Posts: 7
George

Thanks for that.

As a complete novice, what benefits/disadvantages are there to using the switch method over the nested if's (or vice versa)?