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

156
Views
The reduce fuction sum isn't reducing the elements in an array to a single element?

let newton = ["1", " 2", " 3", " 4", " 5"];

let newton2 = newton.reduce(sum);

function sum(total, value) {
  return (total + value);
}
document.getElementById("demo").innerHTML = newton2;
<p id="demo"></p>

So...I have used the reduce function (sum) and its not summing the elemenets in the array and displaying it in a single element, Rather its displaying the elements in the array again.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It's working correctly if you use number instead of strings; let newton = [1,2,3,4,5];

Alternatively you can parse string to integer if that's what you want return(total + parseInt(value)); but then you have to initialize aggregator as integer giving it an initial value, otherwise it'll still be casted to string; newton.reduce(sum, 0).

about 4 years ago · Juan Pablo Isaza Report

0

You should Cast string to int and then use reduce function.

 function sum(total,value){
     var intVal = parseInt(value);
      return(total + intVal);
    }
about 4 years ago · Juan Pablo Isaza 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!