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

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

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