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
- Start with employee 0. Create
let bestIndex = 0. It isletbecause the loop changes it. Assuming the first employee is the best gives the loop something to compare against. - 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 beforenames.length, since the last valid index is one less than the length. - Compare inside the loop. If
productivities[i]is strictly greater thanproductivities[bestIndex], setbestIndextoi. Strictly greater means that on a tie the earlier employee stays in front. - Compute the bonus after the loop from
salaries[bestIndex], times 15, divided by 100, insideMath.floor. - Print the name
names[bestIndex]on its own line. - 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
letin the loop header, so it exists only inside the loop. - Using
names.lengthin 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
ifbody 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]isundefined, and any comparison withundefinedis 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
bestIndexat 0 with an empty team. The arrays here always hold employees, but with empty arrays,names[0]isundefinedand the bonus isNaN. - 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.
