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

IF greater than value add this

Duniagdra
Registered: Sep 24 2008
Posts: 140
Answered

My attempt as follows:

var CharLev = Number(this.getField("ELC").value);
if (CharLev > 10) then
var ASBoost = 1;
elseif (CharLev > 21) then
var ASBoost = 2;
else
var ASBoost = 0;
endif

Simply put, if the value of field ECL is greater than 10 I want the variable ASBoost to be 1. If the value of field ECL is greater than 20 I want the variable ASBoost to be 2. Otherwise ASBoost would be 0 if neither match.

I keep getting an error at var ASBoost = 1 saying I need ";" that is missing. I know I did something wrong, but what? Is the syntax incorrect?

Thanks in advance for helping me in this,
Jim

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

My Product Information:
Acrobat Pro 8.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
var CharLev = Number(this.getField("ELC").value);
FWIW, the following is faster, smaller, simpler, and equivalent:

// Get field value, as a numbervar CharLev = +getField("ELC").value;

But your main problem is incorrect syntax. It should be:

// Get field value, as a numbervar CharLev = +getField("ELC").value; // Declare variable and set the default valuevar ASBoost = 0; // Note: the order of the following statements matterif (CharLev > 10) ASBoost = 1;if (CharLev > 21) ASBoost = 2;

Note that there is no "then", "elseif", or "endif" in the JavaScript if or if/else statements. You can also use "else if", like:

// Get field value, as a numbervar CharLev = +getField("ELC").value; // Declare variablevar ASBoost; // Order mattersif (CharLev > 21) {ASBoost = 2;} else if (CharLev > 10) {ASBoost = 1;} else {ASBoost = 0;   // CharLev <= 10}

If you're going to be doing much JavaScript programming, you should invest in a good language reference, such as David Flanagan's "JavaScript: The Definitive Guide" published by O'Reilly.

George
Duniagdra
Registered: Sep 24 2008
Posts: 140
George_Johnson wrote:
var CharLev = Number(this.getField("ELC").value);
FWIW, the following is faster, smaller, simpler, and equivalent:

// Get field value, as a numbervar CharLev = +getField("ELC").value;

But your main problem is incorrect syntax. It should be:

// Get field value, as a numbervar CharLev = +getField("ELC").value; // Declare variable and set the default valuevar ASBoost = 0; // Note: the order of the following statements matterif (CharLev > 10) ASBoost = 1;if (CharLev > 21) ASBoost = 2;

Note that there is no "then", "elseif", or "endif" in the JavaScript if or if/else statements. You can also use "else if", like:

// Get field value, as a numbervar CharLev = +getField("ELC").value; // Declare variablevar ASBoost; // Order mattersif (CharLev > 21) {ASBoost = 2;} else if (CharLev > 10) {ASBoost = 1;} else {ASBoost = 0;   // CharLev <= 10}

If you're going to be doing much JavaScript programming, you should invest in a good language reference, such as David Flanagan's "JavaScript: The Definitive Guide" published by O'Reilly.

George
Thanks George. I'll definitely look into that book. My budget is just really tight right now so I'm using the Internet as my Reference Guide. I'm going with your shorter, simpler, cleaner code. It gets the job done is less time or clutter.

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

Duniagdra
Registered: Sep 24 2008
Posts: 140
I was just tried combining it with another function:

function AS_BaseRace(Base,Race){// Get field value, as a numbervar CharLev = +getField("ELC").value; // Declare variable and set the default valuevar ASBoost = 0; // Note: the order of the following statements matterif (CharLev > 10) ASBoost = 1;if (CharLev > 21) ASBoost = 2;  var Mod2 = Number(this.getField(Base).value);var Level2 = Number(this.getField(Race).value);event.value = Mod2+Level2+ASBoost;}

I started thinking if I combine the two it would save steps, but it's not totaling. Can I not combine this?

I tried it as a separate function:
function LevelBoost(){// Get field value, as a numbervar CharLev = +getField("ELC").value; // Declare variable and set the default valuevar ASBoost = 0; // Note: the order of the following statements matterif (CharLev > 10) ASBoost = 1;if (CharLev > 20) ASBoost = 2;} function AS_BaseRace(Base,Race){var Mod2 = Number(this.getField(Base).value);var Level2 = Number(this.getField(Race).value);event.value = Mod2+Level2;}

And the following as a custom field script:

AS_BaseRace(Base,Race) + LevelBoost()

And tried increasing the level for ECL above 10 and 20 and the increase of 1 or 2 did not add in. Now what'd I do wrong?

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Instead of trying to use field values and force field values within a funciton, you can pass values to a function for processing and have the funciton return the value from the processing so one would not be restircted to processing given fields within a funciton:

Create the following document level scripts to compute using passed parameter values and return the computed result:

function AS_BaseRace(Base, Race) {Base = Number(Base);Race =Number(Race);return Base + Race;} // end function function LevelBoost(ELC){ELC = Number(ELC);// Declare variable and set the default valuevar ASBoost = 0; // Note: the order of the following statements matterif (ELC> 10) ASBoost = 1;if (ELC > 20) ASBoost = 2;return ASBoost;}

Use the following code for a custom calculation script to set the form fields value:

// define the values for parameters to be passed to functionsvar base = this.getField('Base').value;var race = this.getField('Race').value;var elc = this.getField('ELC').value; // compute event value from the results of the function callsevent.value = AS_BaseRace(base, race) + LevelBoost(elc);

To have your 'AS_BaseRace' function you would need to pass strings with the field names for 'base' and 'race'. But since you are not modifying a value or field or returning any value nothing is accoplished by the 'LevelBoost' function outside of the furncion because of the scope of the 'ASBoost' variable that only exist within the furnction.

To combine your 2 functions into one call one could leave define each document level funciton and add the following 3 document level fucntion:

function ComputeBoost(base, race, elc){return AS_BaseRace(base, race) + LevelBoost(elc);} // end function

And then use the following custom calculation script:
// define the values for parameters to be passed to functionsvar base = this.getField('Base').value;var race = this.getField('Race').value;var elc = this.getField('ELC').value; ComputeBoost(base, race, elc)

This shows how versitle it is to have functions be independent of specific field names because one can build complex computations or actions from a series of simpler functions that could also be used idenpently for other actions and this would allow one to reuse code for many purposes and have one place to correct coding errors.

George Kaiser

Duniagdra
Registered: Sep 24 2008
Posts: 140
gkaiseril, it works great. I had trouble at first but realized it was a typo where ELC needed to be ECL. no biggie, I got and you and George helped me succeed in making this work. Thanks for your trouble and time. It's greatly appreciated.

Jim

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack