JS Code for Averages
Copy link to clipboard
Copied
Hi all,
Curious if anyone might be able to help me with some custom Adobe PDF code. It’s been well over a decade since I have dabbled in Java, so I’m having some issues getting things started.
Document is an interview guide. There are 8 pages. At the bottom of each page there is a drop down box where the interviewer rates the answer on a scale of 1-5. The default value is “na".
I need to calculate an average of ONLY the answered questions as not every question will be answered.
Normally this could just be done as a simple average, but the issue is some pages remain “na” after as not every page will be assigned a score.
For simplicity, the drop down fields on each of the eight pages are named “behave#score” and I need to calculate a percentage of only the answered/selected scores into a field “overallscore”. # = page number.
Anyone have any tips on where to look for source/example code to accomplish this?
This is what I was trying to work with based on some other forum post online.
var nSum = 0; nCnt = 0;
for(var i=1;i<=8;i++)
{
var nVal = this.getField("behave" + i + "score").value;
if(!/^\s*$/.test(nVal) && !isNan(nVal))
{
nCnt++;
nsum += nVal;
}
}
event.value = (nCnt > 0)?nsum/nCnt:"NA";
Copy link to clipboard
Copied
I have also tried this code added to a calculate button. The result field stays empty.
// Function to calculate the percentage
function calculatePercentage() {
var totalQuestions = 8;
var totalScore = 0;
for (var i = 1; i <= totalQuestions; i++)
{
var dropdownValue = getField("behave" + i + "score").value;
if (dropdownValue !== "NA") {
totalScore +=
parseInt(dropdownValue);
}
}
var percentage = (totalScore / (totalQuestions * 5)) * 100;
getField("behaveAverage").value = percentage.toFixed(2) + "%"; } // Attach the function to a button click event or any relevant trigger
Copy link to clipboard
Copied
Check the JavaScript console for errors.
Copy link to clipboard
Copied
Ok, I have it somewhat working with this code:
// Define the total number of questions
var totalQuestions = 8;
// Calculate the average based on selected values
function calculateAverage() {
var totalScore = 0;
var totalSelected = 0;
for (var i = 1; i <= totalQuestions; i++) {
var dropdown = getField("behave" + i + "score");
var selectedValue = dropdown.value;
if (selectedValue !== "NA") {
totalScore += parseInt(selectedValue);
totalSelected++;
}
}
var average = (totalScore / (totalSelected * 5)) * 100;
var averageField = getField("behaveAverage");
averageField.value = average.toFixed(2) + "%";
}
// Attach the calculation function to the drop-down change events
for (var i = 1; i <= totalQuestions; i++) {
var dropdown = getField("behave" + i + "score");
dropdown.setAction("OnBlur", "calculateAverage();");
}
// Initialize the average when the form is opened
calculateAverage();
The only issue now is that the result is returning "NaN%" if any of the fields have NA selected.
Copy link to clipboard
Copied
Go to "behaveAverage" field properties, select Format tab and format field as 'Percentage' with two decimal places.
As 'Custom calculation script' of "behaveAverage" field, use this:
var totalScore = 0;
var totalSelected = 0;
for( var i=1; i<=8; i++){
if(this.getField("behave"+i+"score").valueAsString !== "NA"){
totalSelected++;
totalScore += Number(this.getField("behave"+i+"score").valueAsString);}}
if(totalSelected == 0)
event.value = 0;
else
event.value = totalScore / (totalSelected * 5);

