Skip to main content
Participating Frequently
December 21, 2023
Answered

JavaScript Calculator

  • December 21, 2023
  • 3 replies
  • 3009 views

I have run into an issue, with some of the Scripts, I have several pages, and each page has several text forms that the user can add a number into the field, at the bottom of the page the numbers are totaled into one field and the avg is placed into another.  There are 10 pages, that follow this format. On page 11, I pull the avgs. from each page. Placing each of the avgs. in its field, then that number is multiplied, once the number is multiplied it becomes the "Weighted Value." All of the "Weighted Scores" are then added up, to a field named "GrandTotal," the "GrandTotal" is then divided by 100.

 

The problem I am running into is when I reopen the file I sometimes have to re-enter the Script to make it work. Also after I test the fields, I delete the number from one of the above fields, it zeros out in the results fields and weighted score field, but it leaves a number in the "results field"  The code I am using is:

 

// Custom calculate script
// Get the field values as numbers
var numerator = +getField("GrandTotal").value;
var denominator = 100;

// Calculate and set this field value
if (denominator !== 0) {
event.value = numerator / denominator;
} else {
event.value = "";
}

 

Any help would be awesome.

This topic has been closed for replies.
Correct answer Nesa Nurani

I am attaching it here.


In the "Grand total" you have also picked field "WEAPON_RESULT", uncheck it.

3 replies

Participating Frequently
December 22, 2023

I have also changed the Calculation Order so that they are in the order of the way they are in the pages of the document. The 9 fields do not add up to the proper number. I used 2 as my test number for each field. so the total should be 200 but it is being displayed as 202. I am not sure what is happening, any help would be awesome.

 

Nesa Nurani
Community Expert
Community Expert
December 22, 2023

Can you share file?

Participating Frequently
December 22, 2023
try67
Community Expert
Community Expert
December 21, 2023

Not related, but this part of your code is not needed:

if (denominator !== 0) {

You've defined the denominator to be 100. It can't be zero...

Participating Frequently
December 22, 2023

Thank you, it has been a few years since I did any coding at all.

Bernd Alheit
Community Expert
Community Expert
December 21, 2023

Change the field calculation order.

Participating Frequently
December 22, 2023

For the calculations, I am using the Value as the sum (+) of the following fields. Using the pick button I selected the fields, I wanted to add together. Is there a way to change the order of the calculations when they are selected that way?

 

Bernd Alheit
Community Expert
Community Expert
December 22, 2023

There you can change the order: