Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
12

JavaScript Calculator

Community Beginner ,
Dec 21, 2023 Dec 21, 2023

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.

TOPICS
JavaScript , PDF , PDF forms
2.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Dec 22, 2023 Dec 22, 2023

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 21, 2023 Dec 21, 2023

Change the field calculation order.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 22, 2023 Dec 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?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2023 Dec 22, 2023

There you can change the order:

Bild9.jpgexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 21, 2023 Dec 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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 22, 2023 Dec 22, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 22, 2023 Dec 22, 2023

Picture 2, shows using the sum (+).expand imagePicture 1, shows resultsexpand imagePicture 3, shows items selected.expand imageI 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.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2023 Dec 22, 2023

Can you share file?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 22, 2023 Dec 22, 2023

I am attaching it here.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2023 Dec 22, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 22, 2023 Dec 22, 2023
LATEST

Thank you, I was looking too close at it, but kept missing it I guess, couldn't see the forest from the trees. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines