How to get the JQuery average?

0

Hello I'm starting to work with JQuery I have to get the average of three grades, until now I have nothing but the sum of 3 values ... I would like your help to make the average I have looked for examples and I can not do it I leave you the code that I'm using.

<!DOCTYPE html>
<html lang="es">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Funciones jQuery con parámetros</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  </head>
  <body>

    <div class="container">
      <div class="row">

        <div class="col-xs-6">
            <h2>Función con 2 parámetros - Suma</h2>
            <form>
                <div class="form-group">
                    <label for="valorA">Introduce primer valor</label>
                    <input type="text" name="valorA" id="valorA" class="form-control"/>
                    <label for="valorb">Introduce segundo valor</label>
                    <input type="text" name="valorB" id="valorB" class="form-control"/>
                    <label for="valorb">Introduce tercer valor</label>
                    <input type="text" name="valorC" id="valorC" class="form-control"/>
                </div>
            </form>

            <button id="calcula" class="btn btn-default">Calcular</button>        
        </div>
      </div>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript">
        jQuery(document).ready(function() {


            //FUNCIÓN DE SUMA
            function operacion(a,b,c){                  
                var resultado=a+b+c;                                
                alert('La suma de '+a+' más '+b+' más '+c+'es igual a ' +resultado); 
            }

            jQuery('#calcula').click(function(){                    
                    var num1 = parseInt(jQuery('#valorA').val(),10); 
                    var num2 = parseInt(jQuery('#valorB').val(),10);
                    var num3 = parseInt(jQuery('#valorC').val(),10); 
                    var promedio;

                    operacion(num1,num2,num3);          
            });

        });     
    </script>
  </body>
</html>
    
asked by R.C. Ana 13.02.2018 в 23:18
source

2 answers

3

How about this, do it with simple validations.

<!DOCTYPE html>
<html lang="es">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Funciones jQuery con parámetros</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  </head>
  <body>

    <div class="container">
      <div class="row">

        <div class="col-xs-6">
            <h2>Función con 2 parámetros - Suma</h2>
            <form>
                <div class="form-group">
                    <label for="valorA">Introduce primer valor</label>
                    <input type="text" name="valorA" id="valorA" class="form-control"/>
                    <label for="valorb">Introduce segundo valor</label>
                    <input type="text" name="valorB" id="valorB" class="form-control"/>
                    <label for="valorb">Introduce tercer valor</label>
                    <input type="text" name="valorC" id="valorC" class="form-control"/>
                </div>
            </form>

            <button id="calcula" class="btn btn-default">Calcular</button>        
        </div>
      </div>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript">
        jQuery(document).ready(function() {


            //FUNCIÓN DE SUMA
            function operacion(a,b,c){                  
                var resultado = 0;
                resultado = parseFloat(a) + parseFloat(b) + parseFloat(c);
                if (isNaN(resultado)) {
                    alert("Introduce sólo números");
                } else  {
                    if (resultado == 0) {
                        alert("Resultado es cero.")
                    } else {
                        resultado = resultado/3;
                        alert("El resultado es:" + resultado);
                    }
                }
            }

            jQuery('#calcula').click(function(){                    
                    var num1 = $("#valorA").val(); 
                    var num2 = $('#valorB').val();
                    var num3 = $('#valorC').val(); 
                    var promedio;

                    operacion(num1,num2,num3);          
            });

        });     
    </script>
  </body>
</html>
    
answered by 13.02.2018 / 23:34
source
2
//FUNCIÓN DE SUMA
function operacion(a,b,c){                  
   var resultado=a+b+c;
   var promedio = (resultado / 3);                
   alert('La suma de '+a+' más '+b+' más '+c+'es igual a ' +resultado + ' y el promedio es; ' + promedio); 
}
    
answered by 13.02.2018 в 23:31