Skip to content

Instantly share code, notes, and snippets.

@KrzysiekWyka
Last active July 23, 2020 10:12
Show Gist options
  • Save KrzysiekWyka/48201cc82da41861dc5a3576f593c573 to your computer and use it in GitHub Desktop.
Save KrzysiekWyka/48201cc82da41861dc5a3576f593c573 to your computer and use it in GitHub Desktop.
const data = [
{
firstName: "Clare",
lastName: "Winters",
department: "Accounting",
iban: "IL02 4221 4105 8544 0188 471",
salary: "$2000",
payInsurance: false,
},
{
firstName: "Fallon",
lastName: "Gerrelts",
department: "HelpDesk",
iban: undefined,
salary: "$1000",
payInsurance: true,
},
{
firstName: "Susannah",
lastName: "Wayne",
department: "Accounting",
iban: "NL63 UYLP 3268 4623 54",
salary: "$2000",
payInsurance: false,
},
{
firstName: "Monti",
lastName: "Kenealy",
department: "HelpDesk",
iban: "PK42 ILYX FMEN LYAX P7SO 0BWO",
salary: "$2000",
payInsurance: true,
},
{
firstName: "Ceil",
lastName: "Darville",
department: "Accounting",
iban: "SA58 90HH 0HQ4 GQPM XGML VXNN",
salary: "$4000",
payInsurance: true,
},
{
firstName: "Griffy",
lastName: "Bothie",
department: "Accounting",
iban: "MC63 3524 4277 39TK VHB9 VLF0 R87",
salary: "$2000",
payInsurance: true,
},
{
firstName: "Woodie",
lastName: "Petz",
department: "HR",
iban: null,
salary: "$3000",
payInsurance: false,
},
{
firstName: "Sadye",
lastName: "Wigin",
department: "HelpDesk",
iban: "MK59 959R LSNZ 5R4C F98",
salary: "$1000",
payInsurance: true,
},
{
firstName: "Roslyn",
lastName: "Gravie",
department: "Accounting",
iban: "GR70 8120 7965 KAV6 PSPE P49P EJV",
salary: "$1000",
payInsurance: false,
},
{
firstName: "Querida",
lastName: "Church",
department: "HelpDesk",
iban: "MT66 SOXU 4570 7DBU LZ8S A3UY IFAV 6QX",
salary: "$4000",
payInsurance: false,
},
];
const INSURANCE_COST = 200;
// Add yout code below this line
class Employee {
constructor(object) {
this.firstName = object.firstName;
this.lastName = object.lastName;
this.department = object.department;
this.iban = object.iban;
this.salary = object.salary;
this.payInsurance = object.payInsurance;
}
get salaryAsNumber() {
return +this.salary.substring(1, this.salary.length);
}
get salaryDecresedByInsuranceCosts() {
const insuranceCosts = this.payInsurance ? INSURANCE_COST : 0;
return this.salaryAsNumber - insuranceCosts;
}
compareByFirstAndLastName(firstName, lastName) {
return firstName === this.firstName && lastName === this.lastName;
}
}
class Employees {
constructor(rawData) {
this.data = rawData.map((item) => new Employee(item));
}
findEmployeeByFirstAndLastName(firstName, lastName) {
return this.data.findIndex((employee) =>
employee.compareByFirstAndLastName(firstName, lastName)
);
}
updateEmployeeByFirstAndLastName(firstName, lastName, newDepartment) {
const index = this.findEmployeeByFirstAndLastName(firstName, lastName);
this.data[index].department = newDepartment;
}
fireEmployeeByFirstAndLastName(firstName, lastName) {
const index = this.findEmployeeByFirstAndLastName(firstName, lastName);
this.data.splice(index, 1);
}
addEmployee(employee) {
this.data.push(employee);
}
get insuranceToPay() {
return this.data.reduce(
(sum, { payInsurance }) => sum + (payInsurance ? INSURANCE_COST : 0),
0
);
}
get groupedByDepartments() {
return this.data.reduce((prev, curr) => {
const { department } = curr;
const existingDepartment = prev[department];
if (existingDepartment) {
existingDepartment.push(curr);
return { ...prev, [department]: existingDepartment };
}
return { ...prev, [department]: [curr] };
}, {});
}
get groupedByDepartmentsWithDecresedSalary() {
const grouped = this.groupedByDepartments;
const departmentsWithSalary = {};
Object.keys(grouped).forEach((departmentName) => {
const employees = grouped[departmentName];
departmentsWithSalary[departmentName] = employees.reduce(
(sum, { salaryDecresedByInsuranceCosts }) =>
sum + salaryDecresedByInsuranceCosts,
0
);
});
return departmentsWithSalary;
}
get withoutIBan() {
return this.data.filter((employee) => !employee.iban);
}
get greddiestEmployeesInCompany() {
return Employees.greddiestEmployees(this.data);
}
get greddiestEmployeesByDepartments() {
const grouped = this.groupedByDepartments;
const departmentsWithGreediestEmployees = {};
Object.keys(grouped).map((departmentName) => {
const employees = grouped[departmentName];
departmentsWithGreediestEmployees[
departmentName
] = Employees.greddiestEmployees(employees);
});
return departmentsWithGreediestEmployees;
}
static greediestEmployee(data) {
return data.sort((x, y) => y.salaryAsNumber - x.salaryAsNumber)[0];
}
static greddiestEmployees(data) {
const greediestEmployee = Employees.greediestEmployee(data);
return data.filter(
({ salaryAsNumber }) =>
salaryAsNumber === greediestEmployee.salaryAsNumber
);
}
}
const employees = new Employees(data);
// Griffy Bothie to IT
employees.updateEmployeeByFirstAndLastName("Griffy", "Bothie", "HR");
console.log(employees.data);
// Fire Monti Kenealy
employees.fireEmployeeByFirstAndLastName("Monti", "Kenealy");
console.log(employees.data);
// Add employee
// Sum all insurance
console.log(employees.insuranceToPay);
// Group by department
console.log(employees.groupedByDepartments);
// Decreased salaries by insurance cost
// Salary per department
console.log(employees.groupedByDepartmentsWithDecresedSalary);
// Without iban
console.log(employees.withoutIBan);
// Greedies employyes in company & by departments
console.log(employees.greddiestEmployeesInCompany);
// Greedies employyes in departments
console.log(employees.greddiestEmployeesByDepartments);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment