Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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?

<script>

function calcSum(){

for (j=5;j<6;j++){

sum=0

for (i=1;i<13;i++) sum+=1*document.getElement

document.getElementById("n

}

}

</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?

function calcSum(){

for (j=5;j<6;j++){

sum=0

for (i=1;i<13;i++) sum+=1*document.getElement

document.getElementById("n

}

}

</script>

document.getElementById("n

= Math.round(sum/(j*i), 2)

-r-

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 ? Then it gives an error of NaN. How can I avoid that?

function calcSum(){

for (j=5;j<6;j++){

sum= 0;

ints =0;

for (i=1;i<13;i++) {

var value = 1*document.getElementById(

if(!isNaN(value)) { ints++; sum += value;}

}

document.getElementById("n

= ints > 0 ? sum : 0;

}

}

</script>

-r-

. . . .innerHTML=sum/(i-1)

I tried to put Roonaan's rounding feature into it, also, like this:

. . . .innerHTML=Math.round(sum/

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?

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.

= ints > 0 ? sum : 0;

To

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

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?

<script>

function calcSum(){

for (j=5;j<6;j++){

sum= 0;

ints =0;

for (i=1;i<13;i++) {

var value = 1*document.getElementById(

if(!isNaN(value)) { ints++; sum += value;}

}

document.getElementById("n

}

}

</script>

Thanks!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Lets try:

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

Regards

-r-