Start learning free

LESSON 97 OF 291 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V2 in Scala: 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:

        val name1 = "John Doe"
        val productivity1 = 83
        val salary1 = 135000
        val name2 = "Mary Jane"
        val productivity2 = 133
        val 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 var winnerName: String = ""; var winnerSalary: Int = 0 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 >=.

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

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

STEP 1 OF 10
    Ctrl + Enter to check

    SCALA · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in Scala, explained

    Problem overview

    Two employees compete for the award. Each has a name, a productivity score and a salary, held in six values: name1, productivity1, salary1 and the same three with a 2. The program picks the employee with the higher productivity, then prints their name and their bonus, which is 15% of their salary rounded down. On a tie, employee 1, the one listed first, wins.

    With John Doe at productivity 83 and Mary Jane at 133, Mary Jane wins. Her salary is 335000, and 335000 * 15 / 100 is 50250:

    Mary Jane
    Bonus: $50250
    

    The previous version computed the bonus for one employee. This one adds the decision of who gets it.

    Approach: store the winner, then compute once

    The bonus formula is the same for both employees, so the program shouldn't repeat it in each branch. Instead the if/else only records who won, by copying the winner's name and salary into two variables. After the if/else, one block of code computes the bonus from winnerSalary and prints it.

    The tie rule decides the comparison. Employee 1 should win when the productivities are equal, so the if tests productivity1 >= productivity2. Everything else, meaning employee 2 is strictly more productive, falls to the else.

    Why else and not a second if

    You could write two separate if statements, one for each employee. That makes the program ask the second question even when the first one already settled the winner, and with ties you would have to make sure both conditions can't be true at once. With if/else exactly one branch runs, so there is always exactly one winner and the tie rule lives in a single comparison. It also leaves no gap: whatever the two numbers are, one of the branches is taken.

    Step by step

    1. Declare the two blanks. Create var winnerName: String = "" and var winnerSalary: Int = 0 before the if. They are vars because they get a new value later. They go before the if because a variable declared inside braces exists only inside them.
    2. Write the condition. if (productivity1 >= productivity2) {, and close the whole if/else with its } right away so the braces stay balanced while you fill it in.
    3. Employee 1's branch. Inside the if, assign winnerName = name1 and winnerSalary = salary1. They go together: the bonus needs the salary of the same person whose name is printed.
    4. The else. Add } else {. It runs only when employee 2 is more productive.
    5. Employee 2's branch. Assign winnerName = name2 and winnerSalary = salary2.
    6. The bonus. After the if/else, val bonus = winnerSalary * 15 / 100. Both operands are Int, so / drops the fraction.
    7. Print. println(winnerName), then println("Bonus: $" + bonus).

    The solution in Scala

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

    Notes on the Scala details:

    • Scope. If you declare winnerName inside the if and else blocks and print it afterwards, the compiler reports Not found: winnerName, because each block has its own scope.
    • if is an expression. In Scala an if/else produces a value, so the same logic can be written without var: val (n, s) = if (productivity1 >= productivity2) (name1, salary1) else (name2, salary2). The lesson uses the assignment form to practise var and branches; both give the same result.
    • Parentheses. The condition goes in parentheses, and the braces are needed as soon as a branch holds more than one statement.

    Edge cases and common mistakes

    • Ties. With both productivities at 133 the program prints John Doe and Bonus: $20250. Using > instead of >= would hand the tie to Mary Jane.
    • Employee 1 wins. With productivities 140 and 133 the output is John Doe and Bonus: $20250.
    • Mixing employees. Setting winnerName = name2 but winnerSalary = salary1 prints one person with the other's bonus. Always change both together.
    • Unbalanced braces. Every { needs its }. The usual layout puts the else on the same line as the brace that closes the if body, as in } else {, which makes it easy to see which branch each block belongs to. If the closing braces are missing or misplaced, the program doesn't compile.
    • Copy-pasted formula. Computing the bonus inside each branch works but repeats the formula, so one fix has to be made twice.

    Following the program

    With the starter data, productivity1 >= productivity2 asks whether 83 is at least 133. It is not, so the else branch runs and the two variables change like this:

    MomentwinnerNamewinnerSalary
    After the declarationsempty text0
    After the else branchMary Jane335000

    Only then does the bonus line run, with 335000 * 15 = 5025000 and 5025000 / 100 = 50250. The placeholders from the declarations never reach the output: exactly one branch always runs, so both variables are always overwritten before they are read.

    Testing

    Try these inputs by changing the two productivity values (the salaries stay as they are in the starter data):

    productivity1productivity2Output
    83133Mary Jane, Bonus: $50250
    140133John Doe, Bonus: $20250
    133133John Doe, Bonus: $20250

    Key takeaways

    • Use >= when the first item wins ties.
    • Declare variables before the if when code after it needs them.
    • Let the branches choose, and do the shared calculation once afterwards.
    • 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