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

Calculation of the Average of Fields

New Here ,
Jan 27, 2017 Jan 27, 2017

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:

Screen Shot 2017-01-27 at 10.23.40 AM.png

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?

TOPICS
PDF forms
11.9K
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
LEGEND ,
Jan 27, 2017 Jan 27, 2017

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.

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
New Here ,
Jan 09, 2024 Jan 09, 2024

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();

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 ,
Jan 09, 2024 Jan 09, 2024

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";

 

 

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 ,
Jan 09, 2024 Jan 09, 2024

Thank you so much for the reply!

 

Based on the code, the number should always be a round number.

To be more clear...

  1. The values are calculated by averaging a number of fields into a single number. 
  2. That number is then rounded and populates a total field, in this case Text3.
  3. Once the value is set in the total field (Text3), I want to set a radio dial to equal that number.
  4. If the value of the total field (Text3) = 3, the I want the Choice 3 radio dial to be selected. However, if the total field = 2 I want the Choice2 radio dial selected.

 

How would I achieve this within your construct?

 

Marc

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 ,
Jan 09, 2024 Jan 09, 2024

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";

 

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 ,
Jan 10, 2024 Jan 10, 2024
LATEST

THANK YOU!!!!!!  It works perfectly now!

 

Marc

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