Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
How to find mean of an array using Javascript

I tried to iterate through the array and print the sum. But the output am getting is elements of the array.

 <p id="ans"></p>

    <script>
      var text = "the mean is ";
      function mean() {
        var sum = 0;
        var input = document.getElementsByName("values");
        for (var i = 0; i < input.length; i++) {
          sum += input[i].value;
          text = text + sum;
        }

        document.getElementById("ans").innerHTML = text;
      }
    </script>
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Parse the string values into integers and then sum it. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt This is because in javascript if you adding a number to a string it will be casted to a string. For example:

0 + '1' + '2' // 012

But with parse int:

0 + parseInt('1') + parseInt('2') // 3

Or you can cast to int with a simple plus also:

0 + (+'1') + (+'2') // 3
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda