1 Reply Latest reply on Feb 26, 2011 6:54 PM by gkaiseril

# Need help with if...else calculation in PDF form

Hello, I am hoping someone can help me with a form I am creating. I have only very rudimentary knowledge of JavaScript, but I am trying to create an order form for scarves that come in two colours. Customers will enter the quantity of dark scarves (QD) and the quantity of light scarves (QL). I have a simple calculation to add QD and QL together to get the number of total scarves, TS.

My problem comes when I try to add a more complicated calculation. I want to set the form to calculate a subtotal based on number of scarves requested. If customers order fewer than 10, the price is \$30 each. If they order 11-49, the price is \$25 each and over 50, \$15 each. Here is my code, but nothing happens in the "Cost" field when I update the numbers in the previous fields. I hope someone can assist me because I've spent many hours on this and have gotten nowhere! Thank you so much for any assistance you can offer.

if(TS<10)

{

Cost=(30*TS);

}

else if (TS>=11 && TS<=49);

{

Cost=(25*TS);

}

else

{

Cost=(15*TS);

}

• ###### 1. Re: Need help with if...else calculation in PDF form

You need to know the language syntax.

Semicolons denote the end of code statement or terminate a line of code statement.

JavaScript is an object orientated language, so one needs to identify an object and a property or method to manipulate a value of property.

Are you receiving any error messages on the JavaScript debugging console?

If you want to change a field while that field has the focus of the user, then one uses the event object and its properties or methods.

One also needs to be careful of the use of end points in test and make sure all points are covered.

// get the value of the total scarves field
var TS = this.getField('TotalScarves').value;
// default value
event.value = 0;
if(TS > 0 & TS <= 10) {
// ordered 10 or less scarves
event.value = 30 * TS;
} else if ( TS > 10 & TS <= 49) {
// ordered less than 50 scarves
event.value = 25 * TS;
} else {
// ordered 50 or more scarves
event.value = 15 * TS;
}

An alternative to the 'if'' statement is the switch statement.

var TS = this.getField('TotalScarves').value;
// default value
switch(true) {
case (TS > 0 && TS < 11):
event.value = TS * 30;
break;
case (TS < 50):
event.value = TS * 25;
break;
case (TS >=50):
event.value = TS * 15;
break;
default:
event.value = 0;
break;
}