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

Field containing average function only works when data is engaged on same page. Doesn't work for fields on other pages.

New Here ,
Aug 03, 2016 Aug 03, 2016

I am having an issue with a field set to calculate the average of eleven points fields (points fields 1-8 on page one and 9-11 on page two). My original issue was to get it to exclude points fields when they do not have info entered and adjust the averaging calculation ('divide by' number) accordingly.

I've had someone write the JavaScript code for it, but I just found a glitch I’m not sure if it’s something in the code or what, the person who originally wrote the script can't figure it out either and suggested I post here.

So the average box (located on the 2nd page of the two page pdf) seems to only function accurately if at least one of the points fields on the 2nd page (so, 9, 10, and/or 11) are engaged with data. Any points values entered on the first page, and only the first page, are for some reason being dividing by one more than it should. For example, if I fill out boxes 1, 2, and 3, it divides the total by four. If I fill out just box 7, it divides it by two. If I fill out all 8 boxes on the 1st page, it divides it by nine. This is corrected as soon as I enter points into one or more of the three points boxes on the 2nd page, suddenly the overall average is accurate. But if boxes 9, 10, and 11 do not contain points, then the average is always inaccurate because the 'divide by' number that it's using is one more than it should. I can't figure out how to attach the form to this post, so I've uploaded it to Dropbox: Dropbox - Employee Evaluation.pdf.​

Edited to clarify: The javascript entered does work to exclude blank boxes when points are entered into any of the three points fields on the 2nd page (in addition to any on the 1st page), but the averaging function is not accurate in that it adds 1 to the 'divide by' number in the averaging calculation if points are only entered into fields on the 1st page.

Here is the current code that I have entered into the 'Custom calculation script' box in the 'Calculate' tab of the Test Field Properties menu:

// Initialize variables

var num = 0;

var sum = 0;

// Loop through the input fields

for (var i = 1; i < 12; i++) {

var f = getField("points." + i);

if (f.valueAsString) {

// increment the non-blank field counter

num++;

// add the field value to the running total

sum += +f.value;

}

}

// Calculate the average

if (num) {

event.value = sum / num;

} else {

// All fields are empty, so set to blank

event.value = "";

}

TOPICS
Acrobat SDK and JavaScript , Windows
382
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

correct answers 1 Correct answer

LEGEND , Aug 03, 2016 Aug 03, 2016

I do not see this. Adding some debugging code to the script will display the results as each field is entered:

// Initialize variables
var num = 0;
var sum = 0;

// Loop through the input fields
for (var i = 1; i < 12; i++) {
var f = getField("points." + i);
if (f.valueAsString) {
// increment the non-blank field counter
num++;
// add the field value to the running total
sum += +f.value;
}
}
// debug - show results as form is filled in;
console.show
console.println(sum + " / " + num + " = " + sum / num);
// end d

...
Translate
LEGEND ,
Aug 03, 2016 Aug 03, 2016

I do not see this. Adding some debugging code to the script will display the results as each field is entered:

// Initialize variables
var num = 0;
var sum = 0;

// Loop through the input fields
for (var i = 1; i < 12; i++) {
var f = getField("points." + i);
if (f.valueAsString) {
// increment the non-blank field counter
num++;
// add the field value to the running total
sum += +f.value;
}
}
// debug - show results as form is filled in;
console.show
console.println(sum + " / " + num + " = " + sum / num);
// end debugging;
// Calculate the average
if (num) {
event.value = sum / num;
} else {
// All fields are empty, so set to blank
event.value = "";
}

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
New Here ,
Aug 03, 2016 Aug 03, 2016
LATEST

This fixed it! Thank you very much.

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