Start learning free

LESSON 91 OF 414 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V3 - Project in Scala

Practice

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

    Employee Of The Year V3 in Scala

    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free