Problem overview
Version 2 chose between two employees with a single if/else. Version 3 adds a third, name3, productivity3 and salary3, so the choice has three outcomes. The winner is the employee with the highest productivity, and if several share the highest value, the one listed first wins. The winner's bonus is 15% of their salary, rounded down.
In the starter data the productivities are 83, 133 and 97. Mary Jane is highest, with a salary of 335000, so the output is:
Mary Jane
Bonus: $50250
Approach
Three outcomes need a chain of tests, where each test only runs if the earlier ones failed. Start with employee 1: they win when their productivity is at least that of both others. If that fails, employee 1 is out, and only employees 2 and 3 remain, so one comparison between them settles it. If employee 2 doesn't win that comparison, employee 3 does, and no further test is needed.
As in version 2, the chain only fills in winnerName and winnerSalary. The bonus calculation and printing come after it and are unchanged.
Another way to see it is as a tournament. Employee 1 plays the other two at once. If employee 1 doesn't win, the remaining pair play each other. This takes at most three comparisons, and the shape of the code, one test per candidate except the last, grows by one branch for each extra employee.
Step by step
- Declare the blanks
winnerNameandwinnerSalarywithlet, before the chain, so the lines after it can read them. - Test employee 1. The condition has two comparisons joined with
&&: employee 1 must be at least as productive as employee 2 and at least as productive as employee 3. Both use>=so that a tie goes to the employee listed first. - Store employee 1's name and salary inside that branch.
- Add an
else ifcomparingproductivity2 >= productivity3. It runs only if employee 1 lost, so employee 1 doesn't need to appear in it. - Store employee 2's name and salary in that branch.
- Add the final
else. Employee 1 and then employee 2 both lost, so employee 3 is the winner. - Store employee 3's name and salary.
- Compute the bonus from
winnerSalarywithMath.floor, then print the name and the bonus line.
The solution in JavaScript
const name1 = "John Doe";
const productivity1 = 83;
const salary1 = 135000;
const name2 = "Mary Jane";
const productivity2 = 133;
const salary2 = 335000;
const name3 = "Bob Smith";
const productivity3 = 97;
const salary3 = 210000;
let winnerName, winnerSalary;
if (productivity1 >= productivity2 && productivity1 >= productivity3) {
winnerName = name1;
winnerSalary = salary1;
} else if (productivity2 >= productivity3) {
winnerName = name2;
winnerSalary = salary2;
} else {
winnerName = name3;
winnerSalary = salary3;
}
const bonus = Math.floor(winnerSalary * 15 / 100);
console.log(winnerName);
console.log("Bonus: $" + bonus);
Details worth understanding:
&&is "and". The expression is true only when both sides are true. It also stops early: if the left side is false, the right side isn't evaluated.- Branches are exclusive. JavaScript tests the conditions from top to bottom and runs the body of the first one that is true. It skips the rest of the chain, so at most one branch runs.
- The second condition is shorter on purpose. Reaching the
else ifmeansproductivity1 >= productivity2 && productivity1 >= productivity3was false, so employee 1 is beaten by someone. Comparing employee 2 with employee 3 is enough. - Ties still go to the earlier employee. With productivities 100, 100, 100, employee 1 wins. With 90, 100, 100, the first test fails, the second passes because
100 >= 100, and employee 2 wins over employee 3.
Tracing the starter data
With 83, 133 and 97, the first condition is 83 >= 133 && 83 >= 97. The left side is already false, so the whole condition is false. The program moves to the else if: 133 >= 97 is true, so employee 2 is stored as the winner. The else never runs. The bonus is Math.floor(335000 * 15 / 100), which is 50250.
Edge cases and common mistakes
- Using
>in any of the comparisons. Three equal productivities would then fall through to the lastelseand pick employee 3, breaking the tie rule. - Using
||instead of&&.||means "or", so employee 1 would win after beating only one rival. - Writing a separate
iffor each employee. Withoutelse, several tests can pass in the same run, and the last assignment overwrites the earlier ones. - Chaining the comparison like in mathematics.
productivity1 >= productivity2 >= productivity3doesn't mean what it looks like. JavaScript computes the first comparison, gets a boolean, and compares that boolean with a number. For example,5 >= 4 >= 3isfalse, because it becomestrue >= 3, andtruecounts as 1. - Assigning only one of the pair in a branch, which leaves
winnerSalaryasundefinedand the bonus asNaN.
Testing
- Productivities 150, 100, 90 make employee 1 win, with a bonus of 20250.
- Productivities 83, 133, 97 give Mary Jane and 50250.
- Productivities 10, 20, 30 make employee 3 win, with Bob Smith's bonus of 31500.
- Productivities 90, 90, 90 make employee 1 win.
