Skip to main content
Known Participant
November 7, 2022
Answered

Javascript formula

  • November 7, 2022
  • 1 reply
  • 4532 views

I need to translate the following into javascript for a pdf form I am working on that calculates monthly costs based on two different rate factors. 

 

Can you help translate the following into javascritp?

 

if (cost) is < or = 130000 multiply by .02037977 else if cost is > 130000 but < or = 260000 multiply by .02037981

 

Thanks in advance for your help.

 

This topic has been closed for replies.
Correct answer ls_rbls

Sorry I should have been more specific. I can change the long field name to NEXT60MOS or something shorter. How does that effect the script?


Like this:

 

if(event.value == "") this.getField("NEXT60MOS").value = 0;

else if(Number(event.value) > 0 && Number(event.value) <= 130000)
this.getField("NEXT60MOS").value = Number(event.value)*.02037977;
else if(Number(event.value) <= 260000)
this.getField("NEXT60MOS").value = Number(event.value)*.02037981;

1 reply

ls_rbls
Community Expert
Community Expert
November 7, 2022

In order to assist you better you must define a few more details.

 

For example, what is (cost) ? 

 

Is this value taken from a selection that a user choose from a dropdown menu? 

 

An export value that you whish to factor in in your equation when the user ticks a checkbox or radio  button?

 

Or is it a numerical value that the user types in in a texfield object?

 

And what do you want the field that will display the total to do when the (cost) is blank or null?

Known Participant
November 7, 2022

Thanks for your quick reply. The Cost will be a numerical value that the user would type into a texfield.

 

If that field is left blank or is 0 then the total field I would want to show 0 as well.  

ls_rbls
Community Expert
Community Expert
November 8, 2022

If I understood correctly, you need the user to type in a numerical value in the designated (cost) field.

 

You may use different methods with Acrobat JavaScript, for example, a Validation script, a Custom Format script, or a Custom Keystroke script (among many other approaches)

 

I you are new to Acrobat JavaScript, the quickest and simplest way (in my personal opinion) is to do this as a custom calculation script.

 

Below is an example that will execute the script from the event target field (cost).

 

The script declares a variable for the event value (the multiplicand in this case).

 

And it also performs a conditional comparisson of the values entered in that field.

 

If the event value is 0 or the user deletes the numerical value it will display a value of 0 automatically.

 

Then, if the event value is equal or less than 130000 or if it is greater than 130001  the script will execute the desired factorisation and apply the desired multiplier to satisfy that condition.

 

See below:

 

var cost = event.value;

if(cost =="" && cost ==0) event.value = 0;

else {(cost <= 130000 && cost >= 130001) ? event.value = (cost * .02037977) : event.value = (cost * .03037981)}