Sponsored
Sponsored
This approach involves iterating through each employee, checking if their employee_id is odd and their name does not start with 'M'. If both conditions are met, the bonus is set to their full salary; otherwise, it is set to 0. Loop through each employee and apply these checks systematically.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(n), for storing the results.
1class Employee {
2 constructor(employeeId, name, salary) {
3 this.employeeId = employeeId;
4 this.name = name;
5 this.salary = salary;
6 }
7}
8
9function calculateBonus(employees) {
10 return employees.map(employee => {
11 const bonus = (employee.employeeId % 2 !== 0 && !employee.name.startsWith('M')) ? employee.salary : 0;
12 return { employee_id: employee.employeeId, bonus: bonus };
13 });
14}
15
16const employees = [
17 new Employee(2, "Meir", 3000),
18 new Employee(3, "Michael", 3800),
19 new Employee(7, "Addilyn", 7400),
20 new Employee(8, "Juan", 6100),
21 new Employee(9, "Kannon", 7700)
22];
23
24const results = calculateBonus(employees);
25
26console.log("+-------------+-------+");
27console.log("| employee_id | bonus |");
28console.log("+-------------+-------+");
29results.forEach(({ employee_id, bonus }) => {
30 console.log(`| ${employee_id.toString().padStart(11)} | ${bonus.toString().padStart(5)} |`);
31});
32console.log("+-------------+-------+");
This JavaScript code uses a class for employees and a function to calculate bonuses based on the given criteria. It utilizes the map function to iterate and apply the bonus rules, then formats and prints the result.
This approach utilizes functional programming paradigms such as map/filter. It maps through each row of data, filters the rows based on the bonus eligibility criteria, and transforms them with the mapped values.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(n), for storing the results.
1class Employee {
2 constructor(employeeId, name
In this JavaScript solution, map is used similarly to apply the transformation across the employee list. The solution calculates the bonus conditionally and stores it in the result array.