# 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?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Research & Development ManagerCommented:
<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>
0
Commented:
Shouldn't you need to divide by j*i?

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

-r-
0
Author Commented:
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?
0
Commented:
<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 ? sum : 0;
}
}
</script>

-r-
0
Research & Development ManagerCommented:
Back to school for me ...  >.<
0
Author Commented:
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?

0
Author Commented:
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.
0
Commented:
Yeah sorry, I was mixed up with other things. Please change:
= ints > 0 ? sum : 0;
To
= ints > 0 ? Math.round(sum/ints, 2) : 0;
0
Author Commented:
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?
0
Commented:
Then it might be possible that Round does not accept the ,2 argument.

Lets try:

= ints > 0 ? (Math.round(100*sum/ints, 2) / 100) : 0;

Regards

-r-
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
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!
0
IT ExpertCommented:
or just

(sum/ints).toFixed(2)
0
Author Commented:
mplungjan:  Thanks. I also appreciate how that code makes it have 2 decimal places even if it would otherwise be a single digit.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.