Manoj Kumar Manoj Kumar -4 years ago 50
jQuery Question

Calculate max and avg of selected check boxes' scores

I need the max and avg of selected scores from the following table. Itried in different ways. But I couldn't. Please help me with this.
I have added the html too.

$(document).ready(function(){
var employees = [];
function employee(id,name,score,mail,other)
{
this.id= id;
this.name=name;
this.score=score;
this.mail=mail;
this.other=other;
this.selected=false;
}
employees.push(new employee(1,'XYZ',114,'abcd@abc.com',""));
employees.push(new employee(2,'ABC',321,'abc@abc.com',""));
employees.push(new employee(3,'','','',''));
$.each(employees, function(index, employee){
var row = $("<tr id='tdata"+index+"'>");
var column ="";
column += "<td><label><input type='checkbox' id='selectBox"+index+"'
class='case'><span></span></label></td>";
column += "<td>"+employee.name +"</td>";
column += "<td id='score"+index+"' class='score'>"+ employee.score+"
</td>";
column += "<td>"+ employee.mail +"</td>";
column += "<td>"+ '' +"</td>";
row.append(column);
$("table#dynamicTable").append(row);
});


this is the html code. In this table, whenever certain checkbox is selected, corresponding scores must be considered and average of them, max among them must be written.

<html>
<body>
<table id="dynamicTable" class="searchtbl">
<tr>
<th><label><input type="checkbox" id="selectall" /><span></span></label>
</th>
<th>Name</th>
<th>Score</th>
<th>Email</th>
<th></th>
</tr>
</table>

<input type="button" id="Calculate" value="Calculate"/>
<label>Average:</label>
<label class="avg-max" id="Average"></label>
<label>Max:</label>
<label class="avg-max" id="Max"></label>

</body>
</html>

Answer Source

when you check the checkbox you will get the average value.

$(document).ready(function(){
var employees = [];

function employee(id,name,score,mail,other)
{
this.id= id;
this.name=name;
this.score=score;
this.mail=mail;
this.other=other;
this.selected=false;
}

employees.push(new employee(1,'XYZ',114,'abcd@abc.com',""));
employees.push(new employee(2,'ABC',321,'abc@abc.com',""));
employees.push(new employee(3,'','','',''));


$.each(employees, function(index, employee){
var row = $("<tr id='tdata"+index+"'>");
var column ="";

column += "<td><label><input type='checkbox' id='selectBox"+index+"' class='case'><span></span></label></td>";
column += "<td>"+employee.name +"</td>";
column += "<td id='score"+index+"' class='score'>"+ employee.score+"</td>";
column += "<td>"+ employee.mail +"</td>";
column += "<td>"+ '' +"</td>";
row.append(column);

$("table#dynamicTable").append(row); 
});
$('#cal').on('click',function(){
max_avg();
});

function max_avg(){
var score =[];
$('input[type=checkbox]:checked').each(function () {
score.push($(this).closest('tr').find('td.score').text());
});

var sum = 0;
for( var i = 0; i < score.length; i++ ){
    if(!isNaN(parseInt( score[i])))
    {
    sum += parseInt( score[i]);
    }
}
var avg = sum/score.length;
var max = Math.max(...score);
console.log(avg+'-'+max);
}

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id='dynamicTable'></table>
<input type='button' id='cal' value='Calculate'>

For more information about max check here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download