tags:

views:

28

answers:

1

I would like to calculate the mean (average) of some numbers. Every value comes from a radiobuttons that is part of a statement. For example:

<div class="question_answer"> 
<div id="statementRow_9" class="clearfix t_a_ui_data_list_content"> 
    <div class="question"> 
        <p>3een tweede edit iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 </p> 
    </div> 
    <div class="answer"> 
        <ul>
            <li><input type="radio" name="statement_9" id="9_0" checked >0</li>
            <li><input type="radio" name="statement_9" id="9_1">1</li>
            <li><input type="radio" name="statement_9" id="9_2">2</li>
            <li><input type="radio" name="statement_9" id="9_3">3</li></ul>
    </div> 
</div> 
<div><hr /></div>  </div> <div class="question_answer"> 
<div id="statementRow_10" class="clearfix t_a_ui_data_list_content"> 
    <div class="question"> 
        <p>3een tweede edit iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 iets 1 </p> 
    </div> 
    <div class="answer"> 
        <ul>
            <li><input type="radio" name="statement_10" id="10_0" checked >0</li>
            <li><input type="radio" name="statement_10" id="10_1">1</li>
            <li><input type="radio" name="statement_10" id="10_2">2</li>
            <li><input type="radio" name="statement_10" id="10_3">3</li></ul>
    </div> 
</div> 
<div><hr /></div> 

The name of the radiobuttons is statement_9 where 9 is the id of the statement and the id is 9_0-9_3 where 9 is the id of the statement and 0 - 3 is the value of the radiobuttons.

The statements are grouped by categrories and the mean has to be calculated per categroy, so not one in general.

Any suggestions? Thanks!!

+1  A: 

A somewhat verbose way of doing it would be like this:

function calculateMeans() {
    var $inputs = $('div.answer input'),
        map = {},
        ret = {};

    $inputs.each(function(){
        if (!map[this.name]) {
            map[this.name] = [];
        }

        map[this.name].push(this.id.substring(this.id.length-1));
    });

    $.each(map, function(key, val) {
        for (var i = 0, j = val.length, sum = 0; i < j; i++) {
            sum += parseInt(val[i]);
        }
        ret[key] = sum / j;
    });

    return ret;
}

This gives an object of the following form (in this case):

{
  statement_9: 1.5,
  statement_10: 1.5
}

There may well be nicer ways of doing this...


Edit To get the mean of all selected input elements, use the following:

var mean = (function(){
    var sum = 0,
        $inputs = $('div.answer input:checked');

    $inputs.each(function(){
        sum += parseFloat(this.id.substring(this.id.length-1));
    });

    return sum / $inputs.length;
})();
lonesomeday
Thanks a lot!! I will test it and give feedback :-)
koko
I think I gave a bad description ... The user has to choose 0, 1, 2 or 3. The answer of every statement in one category has to be taken together and calculated the mean of the answers. For example: Statement 9: 2, statement 10: 1, so the mean is 1,5. How to do that?
koko
Answer updated.
lonesomeday