How to calculate average from a dropdown excluding the "N/A" values

Debasish Choudhury

Have a client that I have built a simple form for that does basic calculations. There are 6 drop downs that all contain N/A, 1, 2, 3. And at the end a overall average is calculated from these drop downs. client is wanting to totally ignore any drop downs that contain N/A meaning if N/A is in 2 of the 6 drop downs then a average should only be calculated on 4 drop downs. I am just lost on the logic or if statement needed to tell the total field to ignore a certain drop down field if user has selected N/A. Here is my code and is not working..Please suggest some ideas.

<html>
<head><script type="text/javascript">
var aFieldNames = new Array();
aFieldNames["N/A"]=0;
aFieldNames["1"]=1;
aFieldNames["2"]=2;
aFieldNames["3"]=3;
var nCount = 0;
var nSum = 0;
event.value = 0;
for(i = 0; i < aFieldNames.length; i++)
{
if(this.getField(aFieldNames[i]).valueAsString != "N/A") 
{
nCount++;
nSum += parseFloat(this.getField(aFieldNames[i]).value);
}
}
if(nCount != 0) 
 {
event.getElementById("bpover").value = nSum / nCount;
 }
</script>
</head>
<body>
<select name="Value[]" onChange="alert(this.value):">
 <option name="N/A">N/A</option>
  <option name="1">1</option>
   <option name="2">2</option>
    <option name="3">3</option>
  </select><br>
   <select name="Value[]" onChange="alert(this.value):">
  <option name="N/A">N/A</option>
   <option name="1">1</option>
   <option name="2">2</option>
   <option name="3">3</option>
   </select><br>
    <select name="Value[]" onChange="alert(this.value):">
   <option name="N/A">N/A</option>
   <option name="1">1</option>
    <option name="2">2</option>
    <option name="3">3</option>
    </select><br>
     <select name="Value[]" onChange="alert(this.value):">
   <option name="N/A">N/A</option>
    <option name="1">1</option>
    <option name="2">2</option>
    <option name="3">3</option>
    </select><br>
    <select name="Value[]" onChange="alert(this.value):">
   <option name="N/A">N/A</option>
   <option name="1">1</option>
    <option name="2">2</option>
     <option name="3">3</option>
    </select><br>
    <br>
   avg <input type="text" name="Average" ID="bpover" readonly>
    </body>
    </html>
Feathercrown

Here you go! @Shakti Phartiyal's answer has an explanation of the things that were wrong.

function calcAvg(){
  //Get all elements with 'class="select"'
  var selects = document.getElementsByClassName("select");
  //Initialize vars
  var avg = 0;
  var count = 0;
  //Calculate average
  for(var i=0;i<selects.length;i++){
    if(selects[i].value!="N/A"){
      count++;
      avg+=Number(selects[i].value);
      //Alert for debugging purposes
      //alert(selects[i].value+" "+avg);
    }
  }
  avg=avg/count;
  //Output average
  document.getElementById("bpover").value=avg;
}
<select class="select" name="Value[]" onChange="calcAvg();">
 <option name="N/A">N/A</option>
  <option name="1">1</option>
   <option name="2">2</option>
    <option name="3">3</option>
  </select><br>
   <select class="select" name="Value[]" onChange="calcAvg();">
  <option name="N/A">N/A</option>
   <option name="1">1</option>
   <option name="2">2</option>
   <option name="3">3</option>
   </select><br>
    <select class="select" name="Value[]" onChange="calcAvg();">
   <option name="N/A">N/A</option>
   <option name="1">1</option>
    <option name="2">2</option>
    <option name="3">3</option>
    </select><br>
     <select class="select" name="Value[]" onChange="calcAvg();">
   <option name="N/A">N/A</option>
    <option name="1">1</option>
    <option name="2">2</option>
    <option name="3">3</option>
    </select><br>
    <select class="select" name="Value[]" onChange="calcAvg();">
   <option name="N/A">N/A</option>
   <option name="1">1</option>
    <option name="2">2</option>
     <option name="3">3</option>
    </select><br>
    <br>
   avg <input type="text" name="Average" id="bpover" readonly>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java - Trying to calculate the average of values coming from a Getter

From Java

How to calculate average of an array for only entered values

From Dev

SQL : how to calculate an average from a query

From Dev

Average excluding specific values from very large list

From Dev

How to calculate an average in Google Sheets excluding outliers which are manually specified in a separate column?

From Dev

Calculate Average of JSON Values

From Dev

How do I average a column of values while excluding rows that contain a certain value from another column? PowerBi

From Dev

How to calculate an average grade from mix of number grades and letter grades whose values are determined via lookup table

From Dev

R: how to create a ranking variable for each subject excluding NA values

From Dev

R - How to make a mean/average of n previous values, excluding current observation (rolling average)

From Dev

how to calculate average of values in a column by considering the information from another column?

From Dev

SAS: Calculate an average excluding the current observation

From Dev

Calculate average values from an array

From Dev

How do I calculate average of last 10 values from a row containing blanks

From Dev

Excel formula for average excluding x highest values

From Dev

calculate average from values in map in scala

From Dev

How to calculate the average from a list of records

From Dev

Calculate average from last 20 values entered without VBA

From Dev

How to calculate n values in a column and the overall average?

From Dev

How to calculate weighted average for 0 values

From Dev

How can I read in values from a text file and calculate how many times a value repeats and then find the average?

From Dev

Calculate maximum, minimum and average from input values

From Dev

How to calculate average values of an array for each class?

From Dev

How calculate average values for lists in R tibble?

From Dev

Calculate the average excluding some values

From Dev

How to calculate average from multiple array in php?

From Dev

How to calculate average etc from table containing frequences of values?

From Dev

PostgreSQL Excluding values from average

From Dev

How to calculate moving average from bottom to up

Related Related

  1. 1

    Java - Trying to calculate the average of values coming from a Getter

  2. 2

    How to calculate average of an array for only entered values

  3. 3

    SQL : how to calculate an average from a query

  4. 4

    Average excluding specific values from very large list

  5. 5

    How to calculate an average in Google Sheets excluding outliers which are manually specified in a separate column?

  6. 6

    Calculate Average of JSON Values

  7. 7

    How do I average a column of values while excluding rows that contain a certain value from another column? PowerBi

  8. 8

    How to calculate an average grade from mix of number grades and letter grades whose values are determined via lookup table

  9. 9

    R: how to create a ranking variable for each subject excluding NA values

  10. 10

    R - How to make a mean/average of n previous values, excluding current observation (rolling average)

  11. 11

    how to calculate average of values in a column by considering the information from another column?

  12. 12

    SAS: Calculate an average excluding the current observation

  13. 13

    Calculate average values from an array

  14. 14

    How do I calculate average of last 10 values from a row containing blanks

  15. 15

    Excel formula for average excluding x highest values

  16. 16

    calculate average from values in map in scala

  17. 17

    How to calculate the average from a list of records

  18. 18

    Calculate average from last 20 values entered without VBA

  19. 19

    How to calculate n values in a column and the overall average?

  20. 20

    How to calculate weighted average for 0 values

  21. 21

    How can I read in values from a text file and calculate how many times a value repeats and then find the average?

  22. 22

    Calculate maximum, minimum and average from input values

  23. 23

    How to calculate average values of an array for each class?

  24. 24

    How calculate average values for lists in R tibble?

  25. 25

    Calculate the average excluding some values

  26. 26

    How to calculate average from multiple array in php?

  27. 27

    How to calculate average etc from table containing frequences of values?

  28. 28

    PostgreSQL Excluding values from average

  29. 29

    How to calculate moving average from bottom to up

HotTag

Archive