Skip to main content
alanm86183530
Participant
July 11, 2018
Answered

Calculating averages but excluding some radio buttons.

  • July 11, 2018
  • 2 replies
  • 2308 views

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:

Call Opening  [radio button 1] [radio button 2] [radio button 3] [radio button 4] [N/A]

Objection handling [radio button 1] [radio button 2] [radio button 3] [radio button 4] [N/A]

Effective Questioning  [radio button 1] [radio button 2] [radio button 3] [radio button 4] [N/A]

Postioning  [radio button 1] [radio button 2] [radio button 3] [radio button 4] [N/A]

Call Closing[radio button 1] [radio button 2] [radio button 3] [radio button 4] [N/A]

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.

This topic has been closed for replies.
Correct answer try67

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;

2 replies

Inspiring
July 11, 2018

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.

alanm86183530
Participant
July 12, 2018

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?

try67
Community Expert
Community Expert
July 11, 2018

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

alanm86183530
Participant
July 12, 2018

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:

alanm86183530
Participant
July 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;


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?