Advertisement
Btwonu

Company

Oct 15th, 2020 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Company {
  2.   constructor() {
  3.     this.departments = [];
  4.   }
  5.  
  6.   addEmployee(...params) {
  7.     let [name, salary, position, department] = params;
  8.     let departmentPresent = false;
  9.  
  10.     // invalid input test
  11.     const test = (ele) => ele === undefined || ele === null || ele === '';
  12.     let isInvalidInput = params.some(test) || salary < 0;
  13.  
  14.     if (isInvalidInput) {
  15.       throw new Error('Invalid input!');
  16.     }
  17.  
  18.     // create new employee
  19.     const empObj = {
  20.       name,
  21.       salary,
  22.       position,
  23.       department,
  24.     };
  25.  
  26.     // check if department exists
  27.     this.departments.forEach((depObj) => {
  28.       if (depObj.name === department) {
  29.         departmentPresent = true;
  30.  
  31.         // if department is already present
  32.         // add the new employee to it
  33.         depObj.employees.push(empObj);
  34.         return;
  35.       }
  36.     });
  37.  
  38.     // if department doesn't exist, add the new one
  39.     if (!departmentPresent) {
  40.       // create new department object
  41.       const departmentObj = {
  42.         name: department,
  43.         // the new employee is the first one in this department
  44.         // so it's the only one in the employees array
  45.         employees: [empObj],
  46.         totalSalary: function () {
  47.           return (
  48.             this.employees.reduce((acc, cur) => {
  49.               return acc + Number(cur.salary);
  50.             }, 0) / this.employees.length
  51.           );
  52.         },
  53.       };
  54.  
  55.       this.departments.push(departmentObj);
  56.     }
  57.  
  58.     return `New employee is hired. Name: ${name}. Position: ${position}`;
  59.   }
  60.  
  61.   bestDepartment() {
  62.     let output = [];
  63.     let name = '';
  64.  
  65.     // extract the department with the highest salary out
  66.     let bestSalary = this.departments.reduce(
  67.       (highestSalary, currentDepartment) => {
  68.         let currHigh = currentDepartment.totalSalary();
  69.  
  70.         if (currHigh > highestSalary) {
  71.           // save best department's name and avg salary
  72.           name = currentDepartment.name;
  73.           return currHigh;
  74.         }
  75.  
  76.         return highestSalary;
  77.       },
  78.       Number.MIN_SAFE_INTEGER
  79.     );
  80.  
  81.     let nameLine = `Best department is: ${name}`;
  82.     let salaryLine = `Average salary: ${bestSalary.toFixed(2)}`;
  83.     let employeesArr = [];
  84.  
  85.     this.departments.forEach((department) => {
  86.       if (department.name === name) {
  87.         // find the array containing the best department's employees
  88.         employeesArr = department.employees.map((e) => e);
  89.       }
  90.     });
  91.  
  92.     // sort by highest salary descending
  93.     // sort by name
  94.     employeesArr.sort((eA, eB) => {
  95.       return eB.salary - eA.salary || eA.name.localeCompare(eB.name);
  96.     });
  97.  
  98.     // add to output arr
  99.     output.push(nameLine);
  100.     output.push(salaryLine);
  101.     output = output.concat(
  102.       employeesArr.map((e) => `${e.name} ${e.salary} ${e.position}`)
  103.     );
  104.  
  105.     return output.join('\n').trim();
  106.   }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement