Calculate the average of a result group

0

I have a results group and I need to calculate their average, how do I do this in jQuery?

The elements are already in variables and separated by classes, for example:

deztotal = parseInt($this.find( ".dezmembros" ).html());

My HTML:

<input type="text" id="tabelainput" class="setmembros" maxlength="3" name="setmembros"  value="10" disabled>
<input type="text" id="tabelainput" class="outmembros" maxlength="3" name="outmembros"  value="15" disabled>
<input type="text" id="tabelainput" class="novmembros" maxlength="3" name="novmembros"  value="15" disabled>
<input type="text" id="tabelainput" class="dezmembros" maxlength="3" name="dezmembros"  value="12" disabled>
    
asked by anonymous 19.07.2014 / 02:06

2 answers

5

If you already have the values in classes, jQuery already returns something that looks like an array, with all occurrences of that class ... The classic method to handle this is .each() ,

var tot = 0;
var n = 0;
$('.m').each(function(){
   tot += parseInt( $(this).text() );
   n++;
});
var MEDIA = tot/n;

However, for something as simple as calculating a mean, the ideal would really be to use an array, and this is also possible in jQuery, using the function makeArray() ,

var A = $.makeArray( $('.m') );
var Atot = 0;
for(i=0; i<A.length; i++)
    Atot += parseInt(A[i].innerHTML);
var Amedia = Atot/A.length;

See link

NOTES

Javascript is not a language equipped with map / reduce to deal elegantly with arrays , but today, even with a small performance cost , it is already possible to make use of filter functions and aggregation of arrays. The average is a typical aggregate, and can be expressed with these features, see that other jsFiddle :

var A = $.makeArray( $('.m') ).map(function(a) {
    return parseInt(a.innerHTML)
}); // ou $('.m').map(function(){ return  parseInt($(this).html()) }).get();
var soma = A.reduce(function(a,b) {return a + b});
var media = soma / A.length;

This way of expressing an arrays algorithm is still little used, there are people who find it unintelligible, "mathematical thing" ... I try to avoid prejudices. You can, for example, use in formal specifications .

As for accessing the values of <input> tags, the easiest thing in jQuery is .val ... For other tags (typically <span> as exemplified in jsfiddle) using nodeValue can be faster, see link

    
23.05.2017 / 14:37
2

Just highlighting my comment in a previous response.

There are two points that need to be checked to achieve your goal.

The first and most important is to use jQuery.val () because you get the value of the field and not the HTML inside it (which does not even exist). You could even use jQuery.attr () , but let's hammer the nail and do not screw it.

The second point is the selector to be used. You have shown that you are using class of the elements, but have used a class applied to only a single element. Even if you correct the previously mentioned point, you will only receive 12 , referring to the last field, not 52 as an expected sum.

Unfortunately there is no native way of adding N fields automatically, so you'll have to iterate. So much so that even mathematical plugins does so.

Demo no Fiddle

If you can not modify the HTML (which I think is very unlikely), you can use the same badly formulated HTML by simply changing the selector.

Demo no Fiddle

The idea here is to use selectors to accept some special formats like that in a Regular Expression.

Using input[name$=membros] restrict the routine to elements whose name attribute ends with the expression members . As they all end up like this, we keep the 52 result.

    
19.07.2014 / 03:26