Copy link to clipboard
Copied
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 = "";
}
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
Copy link to clipboard
Copied
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 = "";
}
Copy link to clipboard
Copied
This fixed it! Thank you very much.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now