alienCred
alienCred

Reputation: 57

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

Upvotes: 1

Views: 205

Answers (3)

Castri1
Castri1

Reputation: 86

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

Upvotes: 0

Guerric P
Guerric P

Reputation: 31805

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

Upvotes: 1

Daniel
Daniel

Reputation: 1995

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

Upvotes: 2

Related Questions