Start learning free

LESSON 98 OF 291 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V3 in Scala: if / else if / else

Practice Read the full explanation

Three employees compete for the award. Use a chained if / else if / else to pick the most productive one and print their 15% bonus.

You start with:

        val name1 = "John Doe"
        val productivity1 = 83
        val salary1 = 135000
        val name2 = "Mary Jane"
        val productivity2 = 133
        val salary2 = 335000
        val name3 = "Bob Smith"
        val productivity3 = 97
        val salary3 = 210000

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever of the three employees has the highest productivity.
  • If two or more employees share the highest productivity, 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 var winnerName: String = ""; var winnerSalary: Int = 0 before the if, then use an if / else if / else chain to assign them, so both are still in scope for the prints. Employee 1 wins when its productivity is at least that of both others; if not, employee 2 wins when it is at least employee 3's; otherwise employee 3 wins.

Dividing two whole numbers (Int) drops the fraction, so winnerSalary * 15 / 100 is already rounded down: val bonus = winnerSalary * 15 / 100

+ turns the Int into text: println("Bonus: $" + bonus)

        val name1 = "John Doe"
        val productivity1 = 83
        val salary1 = 135000
        val name2 = "Mary Jane"
        val productivity2 = 133
        val salary2 = 335000
        val name3 = "Bob Smith"
        val productivity3 = 97
        val salary3 = 210000

        var winnerName: String = ""
        var winnerSalary: Int = 0
        if (productivity1 >= productivity2 && productivity1 >= productivity3) {
            winnerName = name1
            winnerSalary = salary1
        } else if (productivity2 >= productivity3) {
            winnerName = name2
            winnerSalary = salary2
        } else {
            winnerName = name3
            winnerSalary = salary3
        }
        val bonus = winnerSalary * 15 / 100
        println(winnerName)
        println("Bonus: $" + bonus)

STEP 1 OF 13
    Ctrl + Enter to check

    SCALA · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V3 in Scala, explained

    Problem overview

    Now three employees compete: name1, name2, name3, each with a productivity and a salary. The program finds the one with the highest productivity and prints their name and their bonus, 15% of the salary rounded down. If two or more employees share the highest productivity, the one listed first wins.

    With productivities 83, 133 and 97, Mary Jane (133, salary 335000) wins, and 335000 * 15 / 100 is 50250:

    Mary Jane
    Bonus: $50250
    

    The last version chose between two employees with if/else. This one needs a chain, because there are three possible winners.

    Approach: knock out candidates in order

    With three employees, ask the questions in order:

    1. Is employee 1 at least as productive as both of the others? Then employee 1 wins.
    2. If not, someone else is strictly better than employee 1, so employee 1 is out. Only employees 2 and 3 are left: is employee 2 at least as productive as employee 3? Then employee 2 wins.
    3. Otherwise employee 3 wins, and no further test is needed.

    Every comparison uses >=. That makes the first-listed employee win each tie, whichever employees are tied. The chain only reaches its second test after the first one failed, which is why the second test doesn't need to look at employee 1 again.

    As before, the branches only record the winner's name and salary. The bonus is computed once, after the chain.

    Writing the first test as productivity1 >= productivity2 && productivity1 >= productivity3 is the part that needs care. It is not enough to beat just one rival: the winner has to be at least as good as everyone else. Notice also that the chain is hand-written for exactly three people. A fourth employee would need a longer first test and one more branch, so the code grows with the number of employees.

    Step by step

    1. Declare the blanks. var winnerName: String = "" and var winnerSalary: Int = 0, before the chain so that the lines after it can read them.
    2. First condition. if (productivity1 >= productivity2 && productivity1 >= productivity3) {. The && operator means "and": both comparisons must hold. Write the closing brace of the whole chain right away.
    3. Employee 1's branch. Assign name1 to winnerName and salary1 to winnerSalary.
    4. The else if. Add } else if (productivity2 >= productivity3) {. Employee 1 already lost, so employees 2 and 3 are the only ones left to compare.
    5. Employee 2's branch. Assign name2 and salary2.
    6. The final else. Add } else {. It runs when employee 1 lost and employee 2 lost to employee 3.
    7. Employee 3's branch. Assign name3 and salary3.
    8. Bonus and output. After the chain, val bonus = winnerSalary * 15 / 100, then print the name and the "Bonus: $" line.

    The solution in Scala

    val name1 = "John Doe"
    val productivity1 = 83
    val salary1 = 135000
    val name2 = "Mary Jane"
    val productivity2 = 133
    val salary2 = 335000
    val name3 = "Bob Smith"
    val productivity3 = 97
    val salary3 = 210000
    var winnerName: String = ""
    var winnerSalary: Int = 0
    if (productivity1 >= productivity2 && productivity1 >= productivity3) {
        winnerName = name1
        winnerSalary = salary1
    } else if (productivity2 >= productivity3) {
        winnerName = name2
        winnerSalary = salary2
    } else {
        winnerName = name3
        winnerSalary = salary3
    }
    val bonus = winnerSalary * 15 / 100
    println(winnerName)
    println("Bonus: $" + bonus)
    

    Scala details to keep in mind:

    • Only one branch runs. Scala checks the conditions from top to bottom and runs the first branch whose condition is true. The rest are skipped, even if they would be true too.
    • Short-circuit &&. If the left side of && is false, the right side isn't evaluated. Here that is only a saving, since both sides are simple comparisons.
    • The chain is one expression. else if is just an else whose body is another if. That is why the final else belongs to the whole chain and covers every case not handled above.

    Following the program

    With the starter data the first test asks whether 83 >= 133 and 83 >= 97. Both are false, so the branch is skipped. The second test asks whether 133 >= 97, which is true, so employee 2 is the winner and the final else never runs.

    Testing

    Changing only the productivities (the names and salaries stay as in the starter data) gives these outputs:

    Productivities (1, 2, 3)Output
    83, 133, 97Mary Jane, Bonus: $50250
    150, 133, 97John Doe, Bonus: $20250
    83, 90, 97Bob Smith, Bonus: $31500
    100, 100, 100John Doe, Bonus: $20250
    80, 100, 100Mary Jane, Bonus: $50250

    Edge cases and common mistakes

    • Using > in some tests. The tie rule only holds if every comparison is >=. With a strict > in the first test, a three-way tie would skip employee 1 and pick someone else.
    • Forgetting one comparison. If the first test only compares employees 1 and 2, employee 1 can win with 100 even though employee 3 has 120.
    • A tie between employees 2 and 3 only. In the last row of the table, employee 1 has 80, so the first test fails. The second test is 100 >= 100, which is true, so the earlier of the two, employee 2, wins.
    • Updating only one variable. Every branch must set both the name and the salary, or the output mixes two people.

    Key takeaways

    • A chain of if, else if and else picks exactly one branch.
    • Combine conditions with && when a candidate must beat several others.
    • Use >= everywhere so that the first-listed employee wins ties.
    • The next version builds on this one.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free