• Global community
• Language:
• Deutsch
• English
• EspaÃ±ol
• FranÃ§ais
• PortuguÃªs
• æ—¥æœ¬èªžã‚³ãƒŸãƒ¥ãƒ‹ãƒ†ã‚£
Dedicated community for Japanese speakers
• í•œêµ­ ì»¤ë®¤ë‹ˆí‹°
Dedicated community for Korean speakers
Exit
• Re: Calculating averages but excluding some radio ...

# Calculating averages but excluding some radio buttons.

Community Beginner ,
Jul 11, 2018 Jul 11, 2018

Copied

I am building a simple call monitoring form. I want the user to be able to select a radio button for each phase of the call, for example:

Where radio button 1 = 1, 2 = 2, 3 = 3, 4 = 4.

Using the built-in functions I created a field that displays the averages of the radio button groups. I've been asked to include an N/A option for each of the sections so that if the user selects radio button 4 for filed one, radio button 3 for field 2 and N/A for the remaining fields the sore won't pick up the calculation for the N/A selections.

Is there a way to exclude any field that has N/A from the calculation using Simplified field notation or some other option?

Please note, I have no experience using SFN or java script.

TOPICS
PDF forms

Views

1.2K

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Community Expert , Jul 12, 2018 Jul 12, 2018

You can use this code to achieve it:

`var fields = ["CallPrepRB", "EffQRB", "ActLisRB", "CallPosRB", "CallCloseRB"];var total = 0;var n = 0;for (var i in fields) {    var f = this.getField(fields);    if (f.valueAsString=="Off") continue;    var v = Number(f.valueAsString.replace("Choice", ""));    total+=v;    n++;}if (n==0) event.value = "";else event.value = total/n;`

11 Replies 11
Community Expert ,
Jul 11, 2018 Jul 11, 2018

Copied

Yes, this can be done using a script. What are the names of the fields involved?

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jul 12, 2018 Jul 12, 2018

Copied

Hi, the fields are:

Fields:

SCORE - Properties = Text Box

Radio button filed name and choice names. please note, choice five is the N/A option.

Field Name: CallPrepRB

• Choice1
• Choice2
• Choice3
• Choice4
• Choice5

Field Name: EffQRB

• Choice1
• Choice2
• Choice3
• Choice4
• Choice5

Field Name: ActLisRB

• Choice1
• Choice2
• Choice3
• Choice4
• Choice5

Field Name: CallPosRB

• Choice1
• Choice2
• Choice3
• Choice4
• Choice5

Field Name: CallCloseRB

• Choice1
• Choice2
• Choice3
• Choice4
• Choice5

Below is an image of the page:

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Jul 12, 2018 Jul 12, 2018

Copied

You can use this code to achieve it:

`var fields = ["CallPrepRB", "EffQRB", "ActLisRB", "CallPosRB", "CallCloseRB"];var total = 0;var n = 0;for (var i in fields) {    var f = this.getField(fields);    if (f.valueAsString=="Off") continue;    var v = Number(f.valueAsString.replace("Choice", ""));    total+=v;    n++;}if (n==0) event.value = "";else event.value = total/n;`

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jul 12, 2018 Jul 12, 2018

Copied

Try67 thank you for spending some time on this and creating the code.  I've entered the code you created into the form, however, the N/A button when selected is calculating in the average. Screenshot included. Am I doing something wrong?

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Jul 12, 2018 Jul 12, 2018

Copied

No, it's my mistake... Change this line:

if (f.valueAsString=="Off") continue;

To:

if (f.valueAsString=="Off" || f.valueAsString=="Choice5") continue;

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jul 12, 2018 Jul 12, 2018

Copied

try67, It worked! Thank you so much. Sending you virtual high fives and seriously good karma.  You are a rock star.

For anyone else looking to do something similar this is the code:

var fields = ["CallPrepRB", "EffQRB", "ActLisRB", "CallPosRB", "CallCloseRB"];

var total = 0;

var n = 0;

for (var i in fields) {

var f = this.getField(fields);

if (f.valueAsString=="Off" || f.valueAsString=="Choice5") continue;

var v = Number(f.valueAsString.replace("Choice", ""));

total+=v;

n++;

if (n==0) event.value = "";

else event.value = total/n;

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Mar 31, 2022 Mar 31, 2022

Copied

i am trying to do something almost identical to the last user but can't make it work. help!

here are my fields: "JK", "WQ", "AP", "I", "CLS", "D",  and "O"

here are the options for the radio button fields: ChoiceNA, Choice1, Choice2, Choice3

here is the code that I entered:

var fields = ["JK", "WQ", "AP", "I", "CLS", "D", "O"];

var total = 0;

var n = 0;

for (var i in fields) {

var f = this.getField(fields);

if (f.valueAsString=="Off" || f.valueAsString=="ChoiceNA") continue;

var v = Number(f.valueAsString.replace("Choice", ""));

total+=v;

n++;

}

if (n==0) event.value = "";

else event.value = total/n;

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Mar 31, 2022 Mar 31, 2022

Copied

Change this line:

var f = this.getField(fields);

To:

var f = this.getField(fields[i]);

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Mar 31, 2022 Mar 31, 2022

Copied

LATEST

It worked. You are a wizard. Thank you.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
LEGEND ,
Jul 11, 2018 Jul 11, 2018

Copied

The "Simplified Field Notation" does not support any conditional JavaScript statements. The "Field is the Average of the following fields" will include the "N/A" choices as a zero value and count in the computation of the average. That leaves only the "Custom calculation script". One can create a variable array and then use JavaScript to place each field's value into an element in the array. Then one can use the "filter" method of the array object to remove the "N/A" items from the array. Now one can compute the average using the array of values with the "N/A" items removed. It is the sum of the elements divided by the number of elements in the array. If there are no elements in the array the average cannot be computed.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jul 12, 2018 Jul 12, 2018

Copied

Gkaiseril, thank you for your response. To be honest, I think this is beyond my current skill level. Do you know of any web resource that demonstrates this?