Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!