• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• Calculate the Average of Selected Dropdown Fields ...

# Calculate the Average of Selected Dropdown Fields Excluding Fields Not Yet Selected

New Here ,
Aug 29, 2022 Aug 29, 2022

Copied

Hey folks,

I have an evaluation form that is pretty straightforward, in which an evaluator determines a score of 13 criteria by selecting a value of 1-10 in a dropdown.  The dropdown has a blank value (perhaps a single space?), then 1-10 as individual options.  I have a text field that calucates the average of these scores through normal Acrobat functionality. The owner of the form, however, would like for the average to be calculated only by the specific dropdown boxes that have an actual selection. So, if the evaluator has only selected 8 values of the 13 possible, the average is calculated on those 8, rather than providing 5 false "zeros" into the calculation.  I hope this makes sense.

The dropdowns are named sequentially (Score_1, Score_2, Score_3...).

My Javascript is not strong, and I'm not sure how best to code a solution.  Any assistance would be appreciated.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

590

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 , Aug 29, 2022 Aug 29, 2022

In dropdown fields properties under 'Option' tab, select 'Commit selected value immediately'.

Use this as 'Custom calculation script' of text field:

var total = 0;
var x = 0;
for(var i=1; i<=13; i++){
if(this.getField("Score_"+i).valueAsString != " "){
x++;
total += Number(this.getField("Score_"+i).value);}}
if(x == 0)
event.value = "";
else
event.value = total/x;

5 Replies 5
Community Expert ,
Aug 29, 2022 Aug 29, 2022

Copied

In dropdown fields properties under 'Option' tab, select 'Commit selected value immediately'.

Use this as 'Custom calculation script' of text field:

var total = 0;
var x = 0;
for(var i=1; i<=13; i++){
if(this.getField("Score_"+i).valueAsString != " "){
x++;
total += Number(this.getField("Score_"+i).value);}}
if(x == 0)
event.value = "";
else
event.value = total/x;

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 ,
Aug 31, 2022 Aug 31, 2022

Copied

You need to check if the denominator (ie. "x") is zero, not the numerator (ie. "total"), as it's division by zero that's not allowed, not dividing zero by something else.

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 ,
Aug 31, 2022 Aug 31, 2022

Copied

Yes, you are right that is the proper way to set it and I have changed it, although in this case it was ok since both 'total' and 'x' are 0 or not 0 at the same time. Thanks 🙂

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 ,
Aug 31, 2022 Aug 31, 2022

Copied

@Nesa Nurani thank you so much.  This was straightforward and informative, and has done the trick!  I appreciate your time.

If you don't mind, could you walk me through the logic of what the script is doing?  I'm trying to learn, so I can better support my team. Thank you so much.

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 ,
Jul 24, 2024 Jul 24, 2024

Copied

LATEST

Hi

I have a similar to the one above, but in my case their are only 4 options in the dropdown list with the dropdown propoerties as follows: 2,1,0 and "ignore".

In my case if a response is 0 I want it to effect the average, but if the response is "ignore" (or the most most suitable equivalent), it needs to be excluded from the average calculation. I am trying to see if this calculation can work in this case.

My first example will take tha verage of 6 fields, but there will be others with more fields.

Many thanks. I am a complete novice on JavaScript.