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

161
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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