Home > Back-end >  How to write a function that increases the salary of the employee with the lowest salary?
How to write a function that increases the salary of the employee with the lowest salary?

Time:12-25

I want to write a function that increases the salary of the employee with the lowest salary. It should: receive data on all employees, find the employee with the lowest salary, send a request for a salary increase to this employee by 20%, if the request was successful - send a notification about the salary increase to the employee with the text

Hello, <name>! Congratulations, your new salary is <new salary>!

If the request fails, send error data to the administrator. Should always return a resolved promise with a boolean value: true if the increase was successful, false otherwise. All functions for getting/changing data are asynchronous and return promises.

I can not pass the test "in case of an error in increasing the salary, it should send a notification to the administrator, but not to the user", how can I do it?

function increaseSalary() {
 return api.getEmployees()
  .then(employeeData => {
    const [minSalaryEmployee] = employeeData.reduce(([minEmployee, minSalary], employee) => {
      const {salary} = employee;
      return (salary < minSalary
        ? [employee, salary]
        : [minEmployee, minSalary]
      );
    }, [null, Infinity]);
    const {id, salary: oldSalary} = minSalaryEmployee;
    const newSalary = oldSalary * 1.2;
    return {id, salary: newSalary};
  })
   .then(({id, salary}) => api.setEmployeeSalary(id, salary))
   .then(({name, id, salary}) => api.notifyEmployee(id, `Hello, ${name}! Congratulations, your new salary is ${salary}!`))
   .catch(e => api.notifyAdmin(e));
}

const api = {
  _employees: [
    { id: 1, name: 'Alex', salary: 120000 },
    { id: 2, name: 'Fred', salary: 110000 },
    { id: 3, name: 'Bob', salary: 80000 },
  ],

  getEmployees() {
    return new Promise((resolve) => {
      resolve(this._employees.slice());
    });
  },

  setEmployeeSalary(employeeId, newSalary) {
    return new Promise((resolve) => {
      this._employees = this._employees.map((employee) =>
        employee.id !== employeeId
          ? employee
          : {
            ...employee,
            salary: newSalary,
          }
      );
      resolve(this._employees.find(({ id }) => id === employeeId));
    });
  },

  notifyEmployee(employeeId, text) {
    return new Promise((resolve) => {
      resolve(true);
    });
  },

  notifyAdmin(error) {
    return new Promise((resolve) => {
      resolve(true);
    });
  },

  setEmployees(newEmployees) {
    return new Promise((resolve) => {
      this._employees = newEmployees;
      resolve();
    });
  },
};

CodePudding user response:

I'm going to guess the test suite isn't mocking api.notifyEmployee to throw, but rather to resolve to false. You can try handling this as follows:

// same
.then(({name, id, salary}) =>
   api.notifyEmployee(id, `Hello, ${name}! Congratulations, your new salary is ${salary}!`)
)
.then(success => {
  if (!success) {
    notifyAdmin("some message");
  }

  return success;
})

Another possible interpretation of the prompt suggests that the "request" that might fail is api.setEmployeeSalary, which may return falsey if the request wasn't approved. Under this interpretation, you might try:

// same
.then(({id, salary}) => api.setEmployeeSalary(id, salary))
.then(employee => {
  if (employee) {
    return api.notifyEmployee(id, `Hello, ${name}! Congratulations, your new salary is ${salary}!`));
  }

  return api.notifyAdmin(e).then(() => false);
})

If neither of these do the trick, then I'm not sure what other interpretation could be possible. I'd try to find a resource that might clarify their return value and throw contracts under success and failure scenarios.

CodePudding user response:

function increaseLowestSalary(employees) {
  // find the employee with the lowest salary
  let lowestSalary = Infinity;
  let lowestSalaryEmployee;
  for (const employee of employees) {
    if (employee.salary < lowestSalary) {
      lowestSalary = employee.salary;
      lowestSalaryEmployee = employee;
    }
  }

  // increase the lowest salary
  lowestSalaryEmployee.salary *= 1.1;
}

const employees = [  { name: 'Alice', salary: 10000 },  { name: 'Bob', salary: 20000 },  { name: 'Charlie', salary: 15000 },];

increaseLowestSalary(employees);
console.log(employees);  // logs [{ name: 'Alice', salary: 11000 }, { name: 'Bob', salary: 20000 }, { name: 'Charlie', salary: 15000 }]

  • Related