Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/08/2016, 21:24
Montes28
 
Fecha de Ingreso: septiembre-2010
Mensajes: 1.853
Antigüedad: 13 años, 7 meses
Puntos: 6
Como puedo mejorar el siguiente codigo

Como puedo mejorar el siguiente codigo

Código Javascript:
Ver original
  1. $(".promedio").change(function(){
  2.           var elementos = new Array();
  3.           $.each($("input[class='promedio']:checked"), function() {
  4.                  var elemento = '#despr_'+this.id;
  5.    
  6.                  var valorInput = $(elemento).val();
  7.  
  8.                  if(valorInput != 0 && valorInput != ''){
  9.                    elementos.push($(elemento).val());
  10.                  }
  11.           });  
  12.  
  13.           var valorPromedio = promedio(elementos);
  14.          
  15.     });
  16.  
  17.      $(".despr").on("input", function() {
  18.           var elementos = new Array();
  19.           $.each($("input[class='promedio']:checked"), function() {
  20.              var elemento = '#despr_'+this.id;
  21.              
  22.              var valorInput = $(elemento).val();
  23.  
  24.  
  25.              if(valorInput != 0 && valorInput != ''){
  26.                 elementos.push($(elemento).val());
  27.              }
  28.           });  
  29.  
  30.              var valorPromedio = promedio(elementos);
  31.  
  32.     });
  33.  
  34.  
  35.  
  36.  
  37.  
  38. function promedio(elementos){
  39.       var total = 0;
  40.  
  41.       for(var i in elementos) { total += parseFloat(elementos[i]); }
  42.       var avg = total / elementos.length;
  43.  
  44.       if (avg !== avg) {
  45.           $('#pro_des_red').val(0);
  46.       }else{
  47.           $('#pro_des_red').val(avg);
  48.       };
  49.  
  50.   return avg;
  51. }