Start learning free

LESSON 103 OF 289 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V2 in TypeScript: if/else Between Two

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: string = "John Doe";
const productivity1: number = 83;
const salary1: number = 135000;
const name2: string = "Mary Jane";
const productivity2: number = 133;
const salary2: number = 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: string, winnerSalary: number; 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: number = Math.floor(winnerSalary * 15 / 100);

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

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

STEP 1 OF 10
    Ctrl + Enter to check

    TYPESCRIPT · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in TypeScript, explained

    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

    1. Declare the two blanks. Use let, because the values are assigned later: winnerName as a string and winnerSalary as a number. They have no value yet.
    2. Write the condition. productivity1 >= productivity2 uses >= rather than >, so a tie keeps employee 1 as the winner.
    3. Store employee 1's name in winnerName, inside the if.
    4. Store employee 1's salary in winnerSalary. The name and the salary always go together, so the branch sets both.
    5. Add the else. It runs when employee 2 is strictly more productive.
    6. Store employee 2's name in winnerName.
    7. Store employee 2's salary in winnerSalary.
    8. Compute bonus below the if/else: Math.floor(winnerSalary * 15 / 100). This is the same formula applied to the winner's salary.
    9. Print the name with console.log(winnerName).
    10. Print the bonus line by joining "Bonus: $" and bonus with +.

    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. let and const belong to the nearest pair of braces. The two variables are declared outside the if, so the code after it can read them.
    • Declared without a value. A let with no initializer holds undefined until 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 print undefined.
    • 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 the else branch 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 Jane and Bonus: $50250.
    • Productivities 140 and 133 print John Doe and Bonus: $20250.
    • Productivities 100 and 100 print John Doe and Bonus: $20250, because of the tie rule.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free