Start learning free

LESSON 100 OF 285 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V2 in JavaScript: Choosing with if/else

Practice Read the full explanation

Two employees are up for the Employee of the Year award. Pick the more productive one and compute their 15% bonus.

You start with:

const name1 = "John Doe";
const productivity1 = 83;
const salary1 = 135000;
const name2 = "Mary Jane";
const productivity2 = 133;
const salary2 = 335000;

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever employee has the higher productivity.
  • If both have the same productivity, employee 1 (the one listed first) wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Declare let winnerName, winnerSalary; before the if, then use if/else to assign them, so both are still in scope for the prints. A tie goes to employee 1, so compare with >=.

Use Math.floor to round the 15% down to a whole number: const bonus = Math.floor(winnerSalary * 15 / 100);

+ turns the number into text: console.log("Bonus: $" + bonus);

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);

STEP 1 OF 10
    Ctrl + Enter to check

    JAVASCRIPT · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in JavaScript, explained

    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

    1. Declare the blanks. Write let winnerName, winnerSalary; with no values. Declare them before the if: a variable declared inside the braces of a block can't be seen after them.
    2. Compare. The condition is productivity1 >= productivity2. The >= makes employee 1 win a tie, as the rules say.
    3. Fill in employee 1. Inside the if, assign name1 to winnerName and salary1 to winnerSalary.
    4. Add the else. It runs only when employee 2 is strictly more productive.
    5. Fill in employee 2 in the same way with name2 and salary2.
    6. Compute the bonus after the whole if/else, using winnerSalary and Math.floor.
    7. 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 hold undefined until a branch assigns them. Exactly one branch runs, so both are always set before the bonus is computed.
    • They are let, not const, because a const must receive its value in the statement that declares it and can't be assigned later.
    • A variable declared with let inside a block is local to the block. Declaring winnerName inside the if would make the later console.log(winnerName) fail with winnerName is not defined.
    • The else needs no condition. Once employee 1 failed >=, employee 2 is strictly higher.

    Edge cases and common mistakes

    • Using > instead of >=. With equal productivity, the else would 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 winnerName but not winnerSalary, the bonus becomes NaN, because undefined * 15 is 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 Jane and Bonus: $50250.
    • Productivities 90 and 90 give John Doe and Bonus: $20250, because employee 1 wins ties.
    • Productivities 100 and 80 give John Doe and Bonus: $20250.
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free