Link to home
Start Free TrialLog in
Avatar of Randall-B
Randall-B

asked on

Get Average of Numbers in a Column of HTML Table

This simple javascript calculates and displays the sum total of the 6th column in a table (which happens to have a header, then 12 rows of data, then a blank row at the bottom, where the sum of column 6 is displayed):

<script>
 function calcSum(){
      for (j=5;j<6;j++){
            sum=0
            for (i=1;i<13;i++) sum+=1*document.getElementById("numTable").rows[i].cells[j].innerHTML;
            document.getElementById("numTable").rows[i].cells[j].innerHTML=sum
     }
}
</script>

Instead of showing the sum of Column 6, however, I would like to show the mean average.  How would I modify the script to do that?
Avatar of Vel Eous
Vel Eous

<script>
 function calcSum(){
      for (j=5;j<6;j++){
            sum=0
            for (i=1;i<13;i++) sum+=1*document.getElementById("numTable").rows[i].cells[j].innerHTML;
            document.getElementById("numTable").rows[i].cells[j].innerHTML=sum/j // <------------
     }
}
</script>
Avatar of Roonaan
Shouldn't you need to divide by j*i?

document.getElementById("numTable").rows[i].cells[j].innerHTML
  = Math.round(sum/(j*i), 2)

-r-
Avatar of Randall-B

ASKER

Tchuki,
   Thanks, but dividing by j gives the wrong math average. For example, if each cell in the column has a "2" in it, the average should be 2.  But this modification gave an average of 4.8 .  Also, what if a cell is blank or simply has &nbsp; ? Then it gives an error of NaN.  How can I avoid that?
SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Back to school for me ...  >.<
It seems to work if I divide by (i-1). I think that is because  i  is the number of data rows plus footer row.  Subtracting 1 gives number of data rows only. So this seems to work:  
     . . .     .innerHTML=sum/(i-1)
   I tried to put Roonaan's rounding feature into it, also, like this:
    . . .     .innerHTML=Math.round(sum/(i-1), 2)
but it is rounding to 1 digit, when I actually want two decimal places.  For example, it is rounding to 3 when the average is 2.77829102934 .  How can I round to 2.78 , instead?

     
Roonan,
   Your script appears to add up a sum, instead of calculating an average.  But it works great for ignoring empty cells to avoid the NaN error.
    How would keep that great feature of working around empty cells, but calculate the average instead of the sum?  (And round to 2 decimal places)?  Based on my prior post, I think it has something to do with (i-1), but that would not account for empty cells, which would make the average come out wrong.  Thanks.
Yeah sorry, I was mixed up with other things. Please change:
= ints > 0 ? sum : 0;
To
= ints > 0 ? Math.round(sum/ints, 2) : 0;
Roonaan,
    Good, that gives the average, but it is still rounding to an single digit (integer) instead of two decimal places.  For example, it rounded 2.346 to simply 3.  But I would like it to round to 2.35 .  How?  
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, that works.  So the complete script is:

<script>
 function calcSum(){
      for (j=5;j<6;j++){
            sum= 0;
            ints =0;
            for (i=1;i<13;i++) {
                var value = 1*document.getElementById("numTable").rows[i].cells[j].innerHTML;
                if(!isNaN(value)) { ints++; sum += value;}
             }
             document.getElementById("numTable").rows[i].cells[j].innerHTML = ints > 0 ? (Math.round(100*sum/ints, 2) / 100) : 0;

     }
}
</script>

Thanks!
or just

(sum/ints).toFixed(2)
mplungjan:  Thanks. I also appreciate how that code makes it have 2 decimal places even if it would otherwise be a single digit.