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

314
Vistas
Javascript : Using .reduce and passing in a function to add strings

I'm trying to use the .reduce method passing in a function and an initial value 'Stack' but I've been getting undefined.

I added a console.log in the reduce method to see what may be going wrong, and it seems to be related to the accumulator, which keeps returning undefined, but I can't understand why.

Thank you for your assistance.

function addingSpace(string1, string2){
  let result = string1 + " " + string2;
  return result;
}

function combineStr(array, callback, iv){
  let result = array.reduce((acc, cv) => {
    callback(acc, cv)
  }, iv);
  return result;
}

let testArr = ['flow', 'is', 'amazing']
console.log(combineStr(testArr, addingSpace, 'Stack')) // 'Stack flow is amazing'

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

To further expand on my comment, the callback function you’re using in Array.prototype.reduce isn’t returning anything, so it defaults to returning undefined.

You will need to return the value so that the accumulator is updated for the next iteration:

return callback(acc, cv);
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