Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to write a function that increases the salary of the employee with the lowest salary?

Polli

New Coder
Good evening, it was necessary 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, ! Congratulations, your new salary is ! 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?

My code:
JavaScript:
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();
    });
  },
};
 

New Threads

Buy us a coffee!

Back
Top Bottom