Skip to main content
Known Participant
April 8, 2021
Answered

Average of two form fields

  • April 8, 2021
  • 4 replies
  • 1527 views

Hi I need some help with the below please

 

event.value = (this.getField("Keel to Truck").value) - ((this.getField("FWD DRAFT").value + (this.getField("AFT DRAFT").value) / 2));

 

I would like find the average of two form fields "FWD DRAFT" and "AFT DRAFT" then subtract that value from another field called "Keel to Truck". The above is giving an answer, but not the correct one. Could someone please point me in the right direction.

 

Thanks. 

This topic has been closed for replies.
Correct answer JR Boulay

If  I correctly understand you should use :

event.value = (this.getField("Keel to Truck").value) - ((this.getField("FWD DRAFT").value + (this.getField("AFT DRAFT").value)) / 2);

 

Instead of:

event.value = (this.getField("Keel to Truck").value) - ((this.getField("FWD DRAFT").value + (this.getField("AFT DRAFT").value) / 2));

4 replies

Nesa Nurani
Community Expert
Community Expert
April 8, 2021

You set it right, you just placed parentheses at wrong places, try like this:

event.value = this.getField("Keel to Truck").value - ((this.getField("FWD DRAFT").value + this.getField("AFT DRAFT").value) / 2);

 

Known Participant
April 8, 2021

Yep - relised that. thank you though for taking the time to look at it. 

 

cheers. 

JR Boulay
Community Expert
JR BoulayCommunity ExpertCorrect answer
Community Expert
April 8, 2021

If  I correctly understand you should use :

event.value = (this.getField("Keel to Truck").value) - ((this.getField("FWD DRAFT").value + (this.getField("AFT DRAFT").value)) / 2);

 

Instead of:

event.value = (this.getField("Keel to Truck").value) - ((this.getField("FWD DRAFT").value + (this.getField("AFT DRAFT").value) / 2));

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
Community Expert
April 8, 2021

"The answer it is returning is 44.75"

This answer is OK if your using:

event.value = (this.getField("Keel to Truck").value) - ((this.getField("FWD DRAFT").value + (this.getField("AFT DRAFT").value) / 2));event.value = (this.getField("Keel to Truck").value) - ((this.getField("FWD DRAFT").value + (this.getField("AFT DRAFT").value) / 2));

 

Otherwise your issue is in the JS calculation formula.

Acrobate du PDF, InDesigner et Photoshopographe
Known Participant
April 8, 2021

sorry I dont understand why the need for repeating the script. In simple terms the formula should be

60-((10+10.5)/2)=49.75 or

keel to truck - ((FWD DRAFT+AFT DRAFT)/2)

 

I actually think what is happening is that JS is dividing the AFT DRAFT by 2, not doubling the FWD DRAFT

Known Participant
April 8, 2021

I have solved the issue - just had one of the last parentheses in the wrong place.

 

Thanks for you help.

cheers. 

Bernd Alheit
Community Expert
Community Expert
April 8, 2021

What values does you get?

Known Participant
April 8, 2021

my test values are as follows

FWD DRAFT = 10.0

AFT DRAFT = 10.5

Keel to Truck = 60.0

 

The answer it is returning is 44.75. It should be returning a value of 49.75

Bernd Alheit
Community Expert
Community Expert
April 8, 2021

Looks like that it used the double value of FWD DRAFT.