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

Multiple If Statements

iclifford
Registered: Mar 15 2009
Posts: 3

I'm using multiple If Statements and I'm getting inconsistent results. Is there a better way to write this so the form updates the return value consistently and accurately?

var p1 = +getField("package").value;
var c1 = +getField("Chapel").value;
var h1 = +getField("honors").value;

if (h1==1 && c1==1) photo=40
if (h1==1 && c1==1) video=200
if (h1==1 && c1==1) both=240

if (h1==1 && c1==0) photo=0
if (h1==1 && c1==0) video=100
if (h1==1 && c1==0) both=100

if (h1==0 && c1==1) photo=40
if (h1==0 && c1==1) video=100
if (h1==0 && c1==1) both=140

if (p1==7) p2=995+both
if (p1==6) p2=895+both
if (p1==5) p2=795+both
if (p1==4) p2=445+video
if (p1==3) p2=645+photo
if (p1==2) p2=555+photo
if (p1==1) p2=445+photo

event.value=p2;

My Product Information:
Acrobat Pro 9.0, Macintosh
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
What are the inconsistant results you are obtaining?

1. You can use the block brackets, '{' and '}', to indicate a block of code to be executed.
2. defne all variabales being used.
3. initialize variables or computed results to an initial state.

event.value == ''; // initialize vlauevar p1 = this.getField("package").value;var c1 = this.getField("Chapel").value;var h1 = this.getField("honors").value;// initialize pricingvar photo = 0;var video = 0;var both = 0; // set pricing for photo, video, or both// honors and chapelif(h1 == 1 && c1 == 1) {photo = 40;video = 200;both = 240;} // honors onlyif(h1 == 1 && c1 == 0) {photo = 0;video = 100;both = 100;} // chapel onlyif(h1 == 0 && c1 == 1) {photo = 40;video = 100;both = 140;} // price package with photo and vedio  if (p1 == 7)event.value = 995 + both; if(p1 == 6)event.value = 895 + both; if(p1 == 5)event.value = 795 + both; if( p1 == 4)event.value = 445 + video; if(p1 == 3)event.value = 645 + photo; if(p1 == 2)event.value = 555 + photo; if(p1 == 1)event.value = 445 + photo;

George Kaiser

iclifford
Registered: Mar 15 2009
Posts: 3
That worked perfectly, the form updates whenever a condition is changed promptly now, Thank you!

While working on this code, I was wondering if it was possible to have more then two && in an If Statement, for instance:if(h1 == 1 && c1 == 1 && p==1)
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Yes, you are just applying a logical "AND" to all the results. If you were mix logical operators it is a good idea to use parentheses to indicate the order of application of the logical test.

George Kaiser