Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Company {
- constructor() {
- this.departments = {};
- //{department:{employees: [{name:, salary:, position:},...{}],totalSalaries:totalsum }, }
- }
- addEmployee(name, salary, position, department) {
- if (
- !name ||
- (!salary && salary !== 0) ||
- !position ||
- !department ||
- salary < 0
- ) {
- throw new Error('Invalid input!');
- }
- if (!this.departments.hasOwnProperty(department)) {
- this.departments[department] = { employees: [], totalSalaries: 0 };
- }
- this.departments[department].employees.push({ name, salary, position });
- this.departments[department].totalSalaries += salary;
- return `New employee is hired. Name: ${name}. Position: ${position}`;
- }
- bestDepartment() {
- let outputText = [];
- let bestDeptName = Object.keys(this.departments).sort(
- (a, b) =>
- this.departments[b].totalSalaries /
- this.departments[b].employees.length -
- this.departments[a].totalSalaries /
- this.departments[a].employees.length
- )[0];
- let bestDeptAvgSalary =
- this.departments[bestDeptName].totalSalaries /
- this.departments[bestDeptName].employees.length;
- let sortedBestEmployees = this.departments[bestDeptName].employees.sort(
- (a, b) => b.salary - a.salary || a.name.localeCompare(b.name)
- );
- outputText.push(`Best Department is: ${bestDeptName}`);
- outputText.push(`Average salary: ${bestDeptAvgSalary.toFixed(2)}`);
- sortedBestEmployees.forEach((empl) => {
- outputText.push(`${empl.name} ${empl.salary} ${empl.position}`);
- });
- return outputText.join('\n');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment