Problem overview
Version 3 adds a third employee, name3 with productivity3 and salary3. As before, the most productive employee wins, ties go to whoever is listed first, and the winner's bonus is 15% of their salary, rounded down. The program prints the winner's name, then the bonus line.
With productivities 83, 133 and 97, Mary Jane is the most productive, so the output is again Mary Jane followed by Bonus: $50250. With three possible winners, a single if/else is no longer enough, so this version uses a chain of conditions.
Approach
Three possible winners need three branches, and the branches are tested in order. The first branch asks whether employee 1 beats or ties both others. If not, employee 1 is out, and only employees 2 and 3 remain. The second branch then needs just one comparison. If that fails too, employee 3 is the only one left, so the last branch is a plain else with no condition.
This works because each branch can rely on what the earlier ones already ruled out. The second condition doesn't mention employee 1 at all, and it doesn't need to: it is only reached when employee 1 has lost. As in the last version, every branch fills winnerName and winnerSalary, and the code below the chain is shared.
Walking through the example: employee 1 has productivity 83, which is below 133, so the first condition is false. The second condition compares 133 with 97 and is true, so employee 2 wins, with the salary 335000 and a bonus of 50250. The final else never runs.
The same result could be written with nested if statements, one inside another. The flat chain reads top to bottom as a list of cases, which is easier to follow and keeps the indentation shallow.
Take productivities 83, 97 and 133 instead. The first condition is false, since 83 is below both others. The second compares 97 with 133 and is false as well, so the final else runs: Bob Smith wins, with a bonus of 31500.
Step by step
- Declare
winnerNameandwinnerSalarywithletbefore the chain, so the lines after it can still read them. - Write the first condition. Employee 1 wins when
productivity1 >= productivity2 && productivity1 >= productivity3. Both comparisons use>=so that a tie goes to the earlier employee, and&&requires both to hold. - Store employee 1's name in
winnerName. - Store employee 1's salary in
winnerSalary. - Add
else if (productivity2 >= productivity3). Employee 1 has lost to somebody, so this one comparison decides between employees 2 and 3, with ties going to employee 2. - Store employee 2's name.
- Store employee 2's salary.
- Add the final
else. When neither of the first two branches ran, employee 3 is the winner. - Store employee 3's name.
- Store employee 3's salary.
- Compute
bonuswithMath.floor(winnerSalary * 15 / 100), after the chain. - Print the winner's name.
- Print the bonus line with
"Bonus: $" + bonus.
The solution in TypeScript
const name1: string = "John Doe";
const productivity1: number = 83;
const salary1: number = 135000;
const name2: string = "Mary Jane";
const productivity2: number = 133;
const salary2: number = 335000;
const name3: string = "Bob Smith";
const productivity3: number = 97;
const salary3: number = 210000;
let winnerName: string, winnerSalary: number;
if (productivity1 >= productivity2 && productivity1 >= productivity3) {
winnerName = name1;
winnerSalary = salary1;
} else if (productivity2 >= productivity3) {
winnerName = name2;
winnerSalary = salary2;
} else {
winnerName = name3;
winnerSalary = salary3;
}
const bonus: number = Math.floor(winnerSalary * 15 / 100);
console.log(winnerName);
console.log("Bonus: $" + bonus);
Details to notice:
- Only one branch runs. An
if/else if/elsechain stops at the first condition that is true, and skips the rest. That is why the second condition can stay short. &&short-circuits. If the left comparison is false, the right one isn't evaluated, and the result is already false.- The same three-line shape. Every branch assigns the same pair of variables. Because the annotations are erased at compile time, nothing stops a branch from forgetting one, so the pattern is worth keeping strictly.
Edge cases and common mistakes
- Ties. With productivities 100, 100 and 100, the first condition is true, so John Doe wins. With 100, 100 and 120, Bob Smith wins. With 90, 120 and 120, the first condition fails and the second is true, so Mary Jane wins, the earlier of the two at 120.
- Using
>in place of>=. A strict comparison breaks the tie rule: with all three productivities at 100, the first two conditions fail and Bob Smith wins instead of John Doe. - Checking only one comparison for employee 1. Writing
productivity1 >= productivity2alone would let employee 1 win even when employee 3 is more productive. - Repeating employee 1 in the second condition. It is unnecessary, since that branch is only reached after employee 1 lost.
Testing
- Productivities 83, 133, 97 print
Mary JaneandBonus: $50250. - Productivities 150, 133, 97 print
John DoeandBonus: $20250. - Productivities 83, 97, 133 print
Bob SmithandBonus: $31500. - Productivities 100, 100, 100 print
John DoeandBonus: $20250.
