Start learning free

LESSON 182 OF 285 · LOOPING OVER ARRAY INDICES

Employee of the Year V4 in JavaScript: Finding the Max with a for Loop

Practice Read the full explanation

A whole team is competing. Use a for loop over the arrays to find the most productive employee, then print their 15% bonus.

You start with:

const names = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"];
const productivities = [83, 133, 97, 150, 120];
const salaries = [135000, 335000, 210000, 250000, 180000];

Build:

Alice Chen
Bonus: $37500

Rules:

  • Index i in all three arrays refers to the same employee.
  • The winner is whichever employee has the highest productivity. If two employees share the highest productivity, the one listed first wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Start with let bestIndex = 0 and loop over the other indices with for (let i = 1; i < names.length; i++). Inside the loop, update the best index only when productivities[i] > productivities[bestIndex]: a strict comparison keeps the first employee on a tie.

Use Math.floor to round the 15% down to a whole number: const bonus = Math.floor(salaries[bestIndex] * 15 / 100);

+ turns the number into text: console.log("Bonus: $" + bonus);

const names = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"];
const productivities = [83, 133, 97, 150, 120];
const salaries = [135000, 335000, 210000, 250000, 180000];

let bestIndex = 0;
for (let i = 1; i < names.length; i++) {
    if (productivities[i] > productivities[bestIndex]) {
        bestIndex = i;
    }
}
const bonus = Math.floor(salaries[bestIndex] * 15 / 100);
console.log(names[bestIndex]);
console.log("Bonus: $" + bonus);

STEP 1 OF 6
    Ctrl + Enter to check

    JAVASCRIPT · LOOPING OVER ARRAY INDICES

    Employee Of The Year V4 in JavaScript, explained

    Problem overview

    Versions 2 and 3 used one set of variables per employee, and a chain of conditions that grew with each new candidate. Version 4 stores the whole team in three parallel arrays: names, productivities and salaries. Index i in all three refers to the same employee. The program finds the most productive employee with a for loop and prints the name and the 15% bonus, rounded down. On a tie, the employee listed first wins.

    The starter team has five people. Alice Chen has the highest productivity, 150, and a salary of 250000, so the bonus is 37500:

    Alice Chen
    Bonus: $37500
    

    Approach

    This is the classic scan for a maximum. Keep the index of the best employee seen so far, and look at every other employee once. If one has a strictly higher productivity, they become the new best. When the loop ends, the stored index points at the maximum.

    Storing the index, not the productivity, is the useful choice: one number gives access to the name, the productivity and the salary of the winner, because the three arrays line up. The scan works for any team size, so adding a sixth employee means adding an element to each array and changing no code.

    Step by step

    1. Start with employee 0. Create let bestIndex = 0. It is let because the loop changes it. Assuming the first employee is the best gives the loop something to compare against.
    2. Loop over the rest. Write for (let i = 1; i < names.length; i++). It starts at 1 because employee 0 is already the candidate, and it stops before names.length, since the last valid index is one less than the length.
    3. Compare inside the loop. If productivities[i] is strictly greater than productivities[bestIndex], set bestIndex to i. Strictly greater means that on a tie the earlier employee stays in front.
    4. Compute the bonus after the loop from salaries[bestIndex], times 15, divided by 100, inside Math.floor.
    5. Print the name names[bestIndex] on its own line.
    6. Print the bonus line, joining "Bonus: $" and the number with +.

    The solution in JavaScript

    const names = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"];
    const productivities = [83, 133, 97, 150, 120];
    const salaries = [135000, 335000, 210000, 250000, 180000];
    
    let bestIndex = 0;
    for (let i = 1; i < names.length; i++) {
        if (productivities[i] > productivities[bestIndex]) {
            bestIndex = i;
        }
    }
    const bonus = Math.floor(salaries[bestIndex] * 15 / 100);
    console.log(names[bestIndex]);
    console.log("Bonus: $" + bonus);
    

    The details:

    • The loop variable is declared with let in the loop header, so it exists only inside the loop.
    • Using names.length in the condition ties the loop to the data. All three arrays have the same length, so any of them would work.
    • The braces around the if body are optional for one statement, but the lesson uses them, which keeps later additions safe.
    • Indexing outside an array does not raise an error in JavaScript. productivities[5] is undefined, and any comparison with undefined is false, which can hide a loop that runs one step too far.

    Complexity

    Time: O(n), where n is the number of employees. The loop visits each employee once and does a constant amount of work for each. Space: O(1). The program stores a few numbers regardless of how many employees there are.

    Tracing the starter data

    With productivities [83, 133, 97, 150, 120], bestIndex starts at 0. At i = 1, 133 beats 83, so it becomes 1. At i = 2, 97 doesn't beat 133. At i = 3, 150 beats 133, so it becomes 3. At i = 4, 120 doesn't beat 150. The winner is index 3: names[3] is Alice Chen and salaries[3] is 250000, so the bonus is 37500.

    Edge cases and common mistakes

    • Using >= in the comparison. A later employee with the same productivity would replace the earlier one, breaking the tie rule. For productivities [100, 100], the winner must be index 0.
    • Looping to <= names.length. The last pass reads past the end of the arrays.
    • Storing the best productivity but not the index. The name and salary then can't be found.
    • Starting bestIndex at 0 with an empty team. The arrays here always hold employees, but with empty arrays, names[0] is undefined and the bonus is NaN.
    • Mismatched arrays. The approach relies on the three arrays having the same order and length.

    Testing

    • Productivities [10, 20, 30] pick the last employee.
    • Productivities [30, 20, 10] pick the first, and the loop never replaces it.
    • Productivities [50, 50, 40] pick index 0, because of the tie rule.
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free