Problem overview
Version 1 computed a bonus for one employee. Version 2 has two candidates, described by name1, productivity1, salary1 and name2, productivity2, salary2. The more productive one wins the award and receives a bonus of 15% of their salary, rounded down. On a tie, employee 1 wins.
In the starter data, Mary Jane has productivity 133 against John Doe's 83, so she wins. Her salary is 335000, and 15% of it is 50250:
Mary Jane
Bonus: $50250
Approach
The bonus formula hasn't changed. What changed is whose salary it uses. The program first decides the winner with an if/else, stores the winner's name and salary in two variables, and then runs the same calculation and printing as before, once, on those variables.
The alternative is to repeat the bonus calculation and printing inside each branch. That works for two employees, but it duplicates code. Storing the winner first keeps one copy of the output code.
Step by step
- Declare the blanks. Write
let winnerName, winnerSalary;with no values. Declare them before theif: a variable declared inside the braces of a block can't be seen after them. - Compare. The condition is
productivity1 >= productivity2. The>=makes employee 1 win a tie, as the rules say. - Fill in employee 1. Inside the
if, assignname1towinnerNameandsalary1towinnerSalary. - Add the
else. It runs only when employee 2 is strictly more productive. - Fill in employee 2 in the same way with
name2andsalary2. - Compute the bonus after the whole
if/else, usingwinnerSalaryandMath.floor. - Print the name, then
"Bonus: $"joined with the bonus.
Tracing the starter data
Follow the program line by line with the starter values. After the declarations, winnerName and winnerSalary are both undefined. The condition compares 83 with 133, and 83 >= 133 is false, so the if body is skipped and the else body runs. It sets winnerName to "Mary Jane" and winnerSalary to 335000.
Then 335000 * 15 is 5025000, dividing by 100 gives 50250, and Math.floor leaves it unchanged. The two console.log calls print the name and Bonus: $50250.
Now swap the productivities so employee 1 has 133 and employee 2 has 83. The condition is true, the if body runs and the else body is skipped. The winner is John Doe with a salary of 135000, and the bonus is 20250. The code after the if/else is identical in both runs; only the two stored values differ. That is the point of the design: the decision is made in one place, and the calculation and printing don't need to know how the winner was chosen.
Building the structure first
The lesson's steps suggest writing the if and its closing brace before filling in the bodies, and adding the else the same way. This avoids a common slip with braces: an opening { with no matching } stops the whole program from running. Each assignment is then added inside a block that already has both braces.
Check the result by changing the data. With productivities of 90 and 90, the program prints John Doe and Bonus: $20250. With 100 and 80 it prints the same. With 83 and 133 it prints Mary Jane and Bonus: $50250.
Writing the winner into variables also makes the program easy to debug. If the printed name is wrong, the fault is in the comparison or in the assignments. If only the bonus is wrong, it is in the salary assignment or the formula.
The solution in JavaScript
const name1 = "John Doe";
const productivity1 = 83;
const salary1 = 135000;
const name2 = "Mary Jane";
const productivity2 = 133;
const salary2 = 335000;
let winnerName, winnerSalary;
if (productivity1 >= productivity2) {
winnerName = name1;
winnerSalary = salary1;
} else {
winnerName = name2;
winnerSalary = salary2;
}
const bonus = Math.floor(winnerSalary * 15 / 100);
console.log(winnerName);
console.log("Bonus: $" + bonus);
Details to notice:
let winnerName, winnerSalary;declares two variables in one statement. Both holdundefineduntil a branch assigns them. Exactly one branch runs, so both are always set before the bonus is computed.- They are
let, notconst, because aconstmust receive its value in the statement that declares it and can't be assigned later. - A variable declared with
letinside a block is local to the block. DeclaringwinnerNameinside theifwould make the laterconsole.log(winnerName)fail withwinnerName is not defined. - The
elseneeds no condition. Once employee 1 failed>=, employee 2 is strictly higher.
Edge cases and common mistakes
- Using
>instead of>=. With equal productivity, theelsewould pick employee 2, breaking the tie rule. - Mixing the pair. Taking the name of one employee and the salary of the other prints the wrong bonus. Both assignments in a branch must use the same employee's numbers.
- Forgetting one assignment. If a branch sets
winnerNamebut notwinnerSalary, the bonus becomesNaN, becauseundefined * 15is not a number. - Putting the printing inside only one branch, so nothing prints when the other one runs.
Testing
- Productivities 83 and 133 with the starter salaries give
Mary JaneandBonus: $50250. - Productivities 90 and 90 give
John DoeandBonus: $20250, because employee 1 wins ties. - Productivities 100 and 80 give
John DoeandBonus: $20250.
