Problem overview
In version 2 two employees compete, and the program must choose between them. The data is two groups of constants: name1, productivity1, salary1 and name2, productivity2, salary2.
The winner is the employee with the higher productivity. On a tie, employee 1 wins. The winner's bonus is 15% of their salary, rounded down, and the output is the winner's name followed by Bonus: $ and the amount. With John Doe at productivity 83 and Mary Jane at 133, Mary Jane wins, and 15% of 335000 is 50250:
Mary Jane
Bonus: $50250
Approach
The bonus formula is the same as in the last version, so the only new problem is deciding whose name and salary to use. An if/else does that. Both branches fill the same two variables, winnerName and winnerSalary, and the code after the if/else uses them without caring which branch ran. The printing and the bonus calculation are written once instead of once per employee.
The variables must exist before the if. A let declared inside the braces of a branch is visible only inside those braces, so the lines after the if couldn't read it.
Another way to write it would be to compute and print the bonus inside each branch. That works for two employees, but it copies the same three lines into both branches, and any later change to the formula or the output would have to be made twice. Choosing the winner first and acting on the result once is shorter.
Walking through the example: productivity1 >= productivity2 compares 83 with 133 and is false, so the else branch runs. It sets winnerName to "Mary Jane" and winnerSalary to 335000. The bonus is then Math.floor(335000 * 15 / 100), which is 50250.
Now a tie: with both productivities at 100, the condition 100 >= 100 is true, so the if branch runs and John Doe wins, with a bonus of 20250. The >= is what encodes the rule that employee 1 wins ties.
The winner is picked by productivity alone. The salaries play no part in the comparison and only feed the bonus once the winner is known.
Step by step
- Declare the two blanks. Use
let, because the values are assigned later:winnerNameas astringandwinnerSalaryas anumber. They have no value yet. - Write the condition.
productivity1 >= productivity2uses>=rather than>, so a tie keeps employee 1 as the winner. - Store employee 1's name in
winnerName, inside theif. - Store employee 1's salary in
winnerSalary. The name and the salary always go together, so the branch sets both. - Add the
else. It runs when employee 2 is strictly more productive. - Store employee 2's name in
winnerName. - Store employee 2's salary in
winnerSalary. - Compute
bonusbelow theif/else:Math.floor(winnerSalary * 15 / 100). This is the same formula applied to the winner's salary. - Print the name with
console.log(winnerName). - Print the bonus line by joining
"Bonus: $"andbonuswith+.
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;
let winnerName: string, winnerSalary: number;
if (productivity1 >= productivity2) {
winnerName = name1;
winnerSalary = salary1;
} else {
winnerName = name2;
winnerSalary = salary2;
}
const bonus: number = Math.floor(winnerSalary * 15 / 100);
console.log(winnerName);
console.log("Bonus: $" + bonus);
Some details worth knowing:
- One
let, two variables.let winnerName: string, winnerSalary: number;declares both in one statement, each with its own annotation. - Block scope.
letandconstbelong to the nearest pair of braces. The two variables are declared outside theif, so the code after it can read them. - Declared without a value. A
letwith no initializer holdsundefineduntil it's assigned. Here both branches assign both variables, so every path reaches the bonus line with real values. The lesson compiles with strict checks off, so the compiler doesn't track this for you: if a branch forgot an assignment, the program would run and printundefined. - Types are erased. The annotations help the compiler and editor but vanish from the JavaScript that runs.
Edge cases and common mistakes
- Using
>instead of>=. With both productivities at 100 theelsebranch runs and Mary Jane wins, against the rule. The correct winner is John Doe. - Declaring the variables inside the branches. They disappear at the closing brace, so the bonus line can't use them.
- Comparing salaries. The winner is chosen by productivity. Salary only affects the bonus amount.
A third approach is to give the two variables starting values, such as the data of employee 1, and use an if only to replace them when employee 2 is strictly better. The if/else form used here states both cases explicitly, so each employee's branch can be read on its own, and no placeholder value ever exists.
Testing
- Productivities 83 and 133 print
Mary JaneandBonus: $50250. - Productivities 140 and 133 print
John DoeandBonus: $20250. - Productivities 100 and 100 print
John DoeandBonus: $20250, because of the tie rule.
