Start learning free

LESSON 90 OF 415 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V2 - Project in Swift

Practice

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

You start with:

        let name1 = "John Doe"
        let productivity1 = 83
        let salary1 = 135000
        let name2 = "Mary Jane"
        let productivity2 = 133
        let 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 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: let bonus = winnerSalary * 15 / 100

Convert the number with String(...) (or use \(...) inside the text): print("Bonus: $" + String(bonus))

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

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

STEP 1 OF 10
    Ctrl + Enter to check

    Employee Of The Year V2 in Swift

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