Copy link to clipboard
Copied
I am creating a form that has several fields (9) for a "Rating" (NA, 1,2,3,4) and want the average of the fields.
I have:

Problem is if the field is blank or NA is chosen, than it is calculated as a ZERO and throws off the average result.
Any ideas?
Copy link to clipboard
Copied
That is a pretty common approach used when software assumes how a calculation should be done. Excel excludes non-numeric data from both the sum and count but throws an error condition when all the input fields are empty. The only way around this is to create a custom calculation script to compute the average and exclude the "N/A" items from the sum and the count of items to be averaged.
I would put the values of the fields as strings into an array, filter out any null entries or non-numeric values form the array and then process the resulting array of values when the length of the array is not zero.
Once could also create a for loop to read each field and only accept numeric values for the count and sun and then compute the average when the count of non-null fields is not zero.
Copy link to clipboard
Copied
I have created a form that has roundedAverage calculation fields to average scores. Everything works, and the rounded value populates in another field.
However, now I want to take that value and select a radio dial, in a group, that represents that value. For example, Text3 (value currently = 3) and I want Choice3 radio dial to be selected. The Javascript I have compiled is below and it won't select the radio dial. What am I doing wrong?
// Custom calculation script for the calculation of average and selection of a radio button
// Function to calculate average, excluding "N.R."
function calculateAverageAndSetRadioButton() {
// Retrieve field values
var values = [];
values.push(this.getField("Angles").value);
values.push(this.getField("Squareness").value);
values.push(this.getField("Depth Management").value);
values.push(this.getField("Post Play").value);
// Calculate average excluding "N.R."
var sum = 0; var count = 0;
for (var i = 0; i < values.length; i++) {
var fieldValue = parseFloat(values[i]);
if (!isNaN(fieldValue) && values[i] !== "N.R.") {
sum += fieldValue;
count++;
}
}
// Calculate average
var average = count > 0 ? sum / count : 0;
// Round the average to the nearest whole number
var roundedAverage = Math.round(average);
// Set the value of the 'averageField' (in this case Text3) with the rounded average
this.getField("Text3").value = roundedAverage;
// Set the radio button based on the value of 'Text3' for Group 11
var radioGroup = this.getField("Group11");
var text3Value = this.getField("Text3").value;
// Define the options or values available in Group11 along with their corresponding indexes
var radioOptions = { "Choice1": 1, "Choice2": 2, "Choice3": 3, "Choice4": 4 };
// Set the radio button value based on the value of Text3
var selectedOption = Object.keys(radioOptions).find(
key => radioOptions[key] === parseInt(text3Value) );
if (selectedOption) {
radioGroup.setValue(selectedOption);
} else {
// Handle the case when the value in Text3 doesn't match any radio button option
// For instance, set a default value if needed
radioGroup.setValue("");
// Set a default value or handle as required } }
// Call the calculateAverageAndSetRadioButton
function on the 'calculate' event calculateAverageAndSetRadioButton();
Copy link to clipboard
Copied
What when value is not round number, for example if "Text3" value is 3.2?
To answer your question you can change radio button choices to numbers, change "Choice1" to 1, "Choice2" to 2...etc, then you can use this:
var radioGroup = this.getField("Group11");
var text3Value = Number(this.getField("Text3").valueAsString);
radioGroup.value = text3Value !== 0 ? text3Value : "Off";
Copy link to clipboard
Copied
Thank you so much for the reply!
Based on the code, the number should always be a round number.
To be more clear...
How would I achieve this within your construct?
Marc
Copy link to clipboard
Copied
If the value of "Text3" field is 2 it will check radio button with "Choice2" if the value is 3 it will check radio button with "Choice3"...etc else it will uncheck all radio buttons.
Without the need to change radio button choices, do it like this:
var radioGroup = this.getField("Group11");
var text3Value = Number(this.getField("Text3").valueAsString);
radioGroup.value = text3Value !== 0 ? "Choice"+text3Value : "Off";
Copy link to clipboard
Copied
THANK YOU!!!!!! It works perfectly now!
Marc
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more