🟨 JavaScript
Medium
🕐 10 min

Summing Bonuses by Department

Goal: to master methods of aggregating and summing data in an array of objects.

💡 Hint for the solution

This task can be solved in several ways.

Method 1: Using Array.prototype.reduce

This method is ideal for “reducing” an array to a single value, such as an object.

  1. Initialization: Start reduce with an empty object {}.
  2. Iteration: At each step, check if the department already exists in your accumulator object.
  3. Logic: If the department doesn’t exist, create it. If it does, add the bonus.

Method 2: Using a loop (for...of or forEach)

A classic approach that is easy to read.

  1. Initialization: Create an empty object result = {}.
  2. Iteration: Loop through the array.
  3. Logic: Inside the loop, apply the same logic as in reduce: check for the department’s existence in result and update the bonus sum.
👀 Solution #1

Solution 1: Using reduce

const sumBonusesByDepartment = (employees) => {
  // Start "reducing" the array with an empty object {}.
  // `acc` is the accumulator that stores the final object.
  // `employee` is the current element being processed.
  return employees.reduce((acc, employee) => {
    const { department, bonus } = employee;
 
    // If the department is not yet in the accumulator, initialize it.
    // Use `|| 0` in case the bonus is not specified.
    if (!acc[department]) {
      acc[department] = 0;
    }
 
    // Add the employee's bonus to the total for their department.
    acc[department] += bonus || 0;
 
    // Return the updated accumulator for the next iteration.
    return acc;
  }, {}); // Initial value of the accumulator.
};

Solution Analysis:

  • Time Complexity: O(n), where n is the number of employees.
  • Space Complexity: O(k), where k is the number of unique departments.
👀 Solution #2

Solution 2: Using a for...of loop

const sumBonusesByDepartment = (employees) => {
  // Create an empty object to store the results.
  const result = {};
 
  // Iterate over each employee in the array.
  for (const employee of employees) {
    const { department, bonus } = employee;
 
    // If the department is not yet in the result object, create it.
    if (!result[department]) {
      result[department] = 0;
    }
 
    // Add the bonus to the sum for the corresponding department.
    result[department] += bonus || 0;
  }
 
  // Return the final object.
  return result;
};

Solution Analysis:

  • Time Complexity: O(n), as we iterate through the array once.
  • Space Complexity: O(k), as memory is used to store the result object.

Task Description

You need to implement the sumBonusesByDepartment function, which takes an array of employees objects. Each object contains information about an employee: name, department, and bonus.

The function should calculate the total sum of bonuses for each department and return an object where the keys are the department names and the values are the total bonus amounts for that department.

Examples

const employees = [
  { name: 'John', department: 'Development', bonus: 1000 },
  { name: 'Mary', department: 'Sales', bonus: 1500 },
  { name: 'Peter', department: 'Development', bonus: 1200 },
  { name: 'Ann', department: 'Sales', bonus: 1800 },
  { name: 'Helen', department: 'HR', bonus: 800 }
];
 
sumBonusesByDepartment(employees);
// Expected result:
// { 'Development': 2200, 'Sales': 3300, 'HR': 800 }

Requirements

  • The function must be named sumBonusesByDepartment.
  • If the input array is empty, the function should return an empty object {}.
  • If an employee object is missing the bonus field, their bonus should be treated as 0.

🧑‍💻 It's not a bug! It's a feature!

The code editor is intentionally hidden on mobile.

Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.

📖 Now: Study the task, think through the solution. Act like a strategist.

💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!