8 ms·
Average age is around 39 var ages = [], sum = 0; $('tr').each(function(a,b) { var age = $(b).find('td:nth-child(7)').text(); if (age !==
by onassar 12y ago
Average age is around 39
var ages = [],
sum = 0;
$('tr').each(function(a,b) {
var age = $(b).find('td:nth-child(7)').text();
if (age !== '') {
ages.push(age);
sum += parseInt(age);
}
});
console.log(sum / ages.length);
Returns 39.10679611650485
- apta 12y agoNo need to store a list of ages. A simple counter should suffice.
- AustinDizzy 12y agoOr just count the number of <tr> elements and subtract one to account for the table header. That's what I did to find the average race. $('tr').length-1
- AustinDizzy 12y agoAnd most were White males. Blacks: 190 (36.89%) White: 230 (44.66%) Hispanics: 92 (17.86%) Other: 2 (0.39%) Total: 515