Multiplication and sum of results of a Foreach

1

Hello StackOverFlow colleagues, I am currently trying to multiply percentages%, but currently I have not been able to perform this operation,

Considering that it is a foreach that loads both inputs .weight and .qualification the times that there is a record I need to do the following operation:

Rating * Weight + Rating * Weight + rating * weight and so on depending on the number of records thrown by the cycle foreach .

An example of data would be the following since I am working with percentages and decimals

I enclose image for greater understanding

My code is as follows

    @foreach (var item in ViewBag.listobjetivos)
            {
                <tr>
                    <td>@item.Nombre_Objetivo</td>
                    <td><input class="peso" type="text" value="@item.Peso_Objetivo" />%</td>
                    <td>@item.Descripcion_Objetivo</td>
                    <td><input class="calificacion" type="text" value='@string.Format("{0:F1}", item.calificacion)' /></td>
                    <td>@item.observacion</td>
              </tr>
            }
<span class="resultado"></span>

I need to multiply the weight that is a percentage% by rating and the same for the other records but I need a total result of the sum of all

Here I leave an image in excel so you can understand me a little better, in the image you see the operation in excel.

  

Example Rating: 1.5 * Weight: 5%, Result: 0.075

I remain attentive to any doubt or novelty.

    
asked by ByGroxD 28.06.2017 в 18:20
source

2 answers

2

Reply edited:

  • For this solution use jQuery, it's up to you if you use it.
  • First I create the functions separately for the sum of the weights and the ratings using a .each .
  • Then in the suma_total capture the return of the functions mentioned above.
  • As you can see I create a function called cantidad_filas since in the image you show there are 5 rows but only 4 are filled, that will depend on you, if the rows are empty or not.

I hope my answer will help you

function suma_total() {
    var sum = 0; 
    var total=0;
    var calificacion = suma_calificacion();
    var peso = suma_peso();   
    var filas = cantidad_filas();   
    console.log('Suma de los pesos        : '+peso);    
    console.log('Suma de las calificacion : '+calificacion);  
    console.log('Numero de filas          : '+filas); 
    total=(calificacion*peso)/100;
    total=total/filas;
    console.log("El promedio es           : "+total);    
    $("#resultado").html(total);    
}

function suma_calificacion() {

var sum = 0;
    $('.calificacion').each(function () {
        sum += Number($(this).val());
    });
    return sum;  
}

function suma_peso() {   
      
var sum = 0;
    $('.peso').each(function () {
        sum += Number($(this).val());
    });
    return sum; 
}

function cantidad_filas() { 

var nFilas = $("#tabla_ventas tr").length;
      var nColumnas = $("#tabla_ventas tr:last td").length;
      var msg = "Filas: "+nFilas+" - Columnas: "+nColumnas;
      //console.log(msg);
      return nFilas; 
}

$(document).ready(function(){
suma_total();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tabla_ventas">
<tbody>                                       
<tr id="dato">
<td>peso <input class="peso" id="peso" value="5"/></td>
<td>calificacion <input class="calificacion" id="calificacion" value="1"/></td>
</tr>
<tr id="dato">
<td>peso <input class="peso" id="peso" value="5"/></td>
<td>calificacion <input class="calificacion" id="calificacion" value="0.5"/></td>
</tr>
<tr id="dato">
<td>peso <input class="peso" id="peso" value="5"/></td>
<td>calificacion <input class="calificacion" id="calificacion" value="1.5"/></td>
</tr>
<tr id="dato">
<td>peso <input class="peso" id="peso" value="5"/></td>
<td>calificacion <input class="calificacion" id="calificacion" value="1.5"/></td>
</tr>
 </tbody>
 </table> 
 <br>
 <div id="">Total :</div>
 <label id="resultado">Total :</label>
 <br>
    
answered by 28.06.2017 / 19:45
source
1

what is the reason why you want to do foreach ??

$("#dale").on('click', function(){
		var peso = $('#peso').val();
    var calificacion = $('#calificacion').val();
    var total = ((peso / 100) * calificacion).toFixed(3);
    document.getElementById('spTotal').innerHTML = total;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="peso" value="" />
<input id="calificacion"  value="" />
<button id="dale">
DALE
</button>
<span id="spTotal"></span>
    
answered by 28.06.2017 в 19:58