Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda