javascript 如何编写一个函数,使工资最低的员工的工资增加?

1tu0hz3e  于 2022-12-25  发布在  Java
关注(0)|答案(2)|浏览(175)

我想写一个函数,增加工资最低的员工的工资。它应该:接收所有员工的数据,查找工资最低的员工,向该员工发送加薪20%的请求,如果请求成功,则向该员工发送有关加薪的通知,并附上以下文本
你好,<name>!恭喜你,你的新薪水是<new salary>
如果请求失败,则向管理员发送错误数据。应始终返回具有布尔值的已解决承诺:如果增加成功,则返回true,否则返回false。所有获取/更改数据的函数都是异步的,并返回承诺。
我无法通过测试“如果加薪出错,它应该向管理员发送通知,而不是向用户发送通知”,我该怎么做?

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();
    });
  },
};
rn0zuynd

rn0zuynd1#

我猜测试套件并不是模拟api.notifyEmployee来抛出它,而是将其解析为false。

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

提示符的另一种可能的解释是,可能失败的“请求”是api.setEmployeeSalary,如果请求未被批准,它可能返回falsey。

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

如果这两种解释都不起作用,那么我不确定还有什么其他的解释,我会尝试找到一种资源来澄清它们的返回值,并在成功和失败的情况下抛出合约。

o7jaxewo

o7jaxewo2#

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 }]

相关问题