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

192
Views
Why does my use of javascript's reduce function return "undefined"

1) I've got an array of objects, called employees.

2) I'm trying to find the highest paid employee in the array.

3) I'm using Javascript's reduce function (inside function findHighestPaid) to do so.

4) Running findHighestPaid(employees) returns "undefined" when I expected it to return the object that has property name: Abelard.

Here's my code.

const employees = [
  {
    name: "Aziz",
    salary: 17050,
  },
  {
    name: "Angela",
    salary: 17060,
  },
  {
    name: "Abelard",
    salary: 17070,
  },
];

function findHighestPaid(arrayOfObjects) {
  let myObject = {
    name: "pretendEmployee",
    salary: 17040,
  };

  arrayOfObjects.reduce(myFunction, myObject);

  function myFunction(acc, item) {
    let personSalary = item.salary;
    let accSalary = acc.salary;
    if (Math.sign(personSalary - accSalary) > 0) {
      console.log(`returning object for ${item.name}`);
      return item;
    } else {
      console.log(`returning object for ${acc.name}`);
      return acc;
    }
  } // end myFunction
}

When I run findHighestPaid(employees) and look in console I see :

returning object for Aziz // (as I expected)

returning object for Angela // (as I expected)

returning object for Abelard // (as I expected)

undefined // (oh oh!!!)

Why does the reduce function return "undefined"?

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

0

the undefined that's being printed at the end is the return value of findHighestPaid() function that you run.

since you're not returning anything it's returning undefined. if your goal is to see the return value of reduce() add a return statement to the function like this:

const employees = [
  {
    name: "Aziz",
    salary: 17050,
  },
  {
    name: "Angela",
    salary: 17060,
  },
  {
    name: "Abelard",
    salary: 17070,
  },
];

function findHighestPaid(arrayOfObjects) {
  let myObject = {
    name: "pretendEmployee",
    salary: 17040,
  };

  return arrayOfObjects.reduce(myFunction, myObject);

  function myFunction(acc, item) {
    let personSalary = item.salary;
    let accSalary = acc.salary;
    if (Math.sign(personSalary - accSalary) > 0) {
      console.log(`returning object for ${item.name}`);
      return item;
    } else {
      console.log(`returning object for ${acc.name}`);
      return acc;
    }
  } // end myFunction
}

console.log(findHighestPaid(employees));

about 4 years ago · Juan Pablo Isaza Report

0

Your code is working perfectly although you don't need Math.sign at all, just return the result:

const employees = [
  {
    name: "Aziz",
    salary: 17050,
  },
  {
    name: "Angela",
    salary: 17080,
  },
  {
    name: "Abelard",
    salary: 17070,
  },
];

function findHighestPaid(arrayOfObjects) {
  let myObject = {
    name: "pretendEmployee",
    salary: 17040,
  };

  return arrayOfObjects.reduce(myFunction, myObject);

  function myFunction(acc, item) {
    let personSalary = item.salary;
    let accSalary = acc.salary;
    if (personSalary - accSalary > 0) {
      console.log(`returning object for ${item.name}`);
      return item;
    } else {
      console.log(`returning object for ${acc.name}`);
      return acc;
    }
  } // end myFunction
}

console.log('Highest paid', findHighestPaid(employees));

about 4 years ago · Juan Pablo Isaza Report

0

you just have to put it like this

function findHighestPaid(arrayOfObjects) {
    return arrayOfObjects.reduce((prev, curr) => {
        if(!prev) {
            return curr;
        }

        return curr.salary > prev.salary ? curr : prev;    
    }, null);
}

const highestPaidEmployee = findHighestPaid(employees);

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!