Start learning free

LESSON 97 OF 285 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V2 in Go: Choosing Between Two with if/else

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:

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

Join text and a number with strconv.Itoa: fmt.Println("Bonus: $" + strconv.Itoa(bonus))

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

var winnerName string
var winnerSalary int
if productivity1 >= productivity2 {
    winnerName = name1
    winnerSalary = salary1
} else {
    winnerName = name2
    winnerSalary = salary2
}
bonus := winnerSalary * 15 / 100
fmt.Println(winnerName)
fmt.Println("Bonus: $" + strconv.Itoa(bonus))

STEP 1 OF 10
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in Go, explained

    Problem overview

    Version 1 computed a bonus for one employee. Version 2 has two candidates, stored as name1, productivity1, salary1 and name2, productivity2, salary2. The more productive employee wins. The program prints the winner's name and then their bonus, 15% of the salary rounded down.

    If the productivities are equal, employee 1, the one listed first, wins. With the starting data, Mary Jane (133) beats John Doe (83), and her bonus is 335000 * 15 / 100 = 50250:

    Mary Jane
    Bonus: $50250
    

    Approach: decide first, then compute once

    There are two ways to structure this. You could write the bonus calculation and the printing in both branches of an if. That duplicates code, and each later change has to be made twice. The lesson uses a cleaner pattern: the if/else only decides who won and records the winner's name and salary in two variables. Everything after it, the bonus and the printing, is written once and works on those two variables.

    The pattern scales: another branch could be added and nothing below the chain would change.

    The two variables also carry over from version 1: winnerSalary takes the place of salary in the bonus formula, and the printing lines stay the same as before.

    Go has no ternary operator (condition ? a : b), so a statement-level if/else is the way to choose between two values. You will often see this shape in Go code: declare the variables, then fill them in on each path.

    Traced on the starting data: 83 >= 133 is false, so the else branch runs, winnerName becomes "Mary Jane" and winnerSalary becomes 335000. The line after the branches then computes 335000 * 15 / 100, which is 50250.

    Step by step

    1. Declare the winner's variables first. var winnerName string and var winnerSalary int start at the zero values, "" and 0. They must exist before the if, because a variable declared inside a branch is gone after its closing brace.
    2. Compare with >=. Write if productivity1 >= productivity2 {. The >= is what hands a tie to employee 1.
    3. Record employee 1's name in winnerName inside the branch.
    4. Record employee 1's salary in winnerSalary. The name and the salary must always be taken from the same employee.
    5. Add the else. In Go it sits on the same line as the closing brace: } else {. It runs when employee 2 is strictly more productive.
    6. Record employee 2's name in winnerName.
    7. Record employee 2's salary in winnerSalary.
    8. Compute the bonus below the closing brace: winnerSalary * 15 / 100, with the multiplication before the integer division.
    9. Print the name with fmt.Println.
    10. Print the bonus line, converting the number with strconv.Itoa before joining it to "Bonus: $".

    The solution in Go

    name1 := "John Doe"
    productivity1 := 83
    salary1 := 135000
    name2 := "Mary Jane"
    productivity2 := 133
    salary2 := 335000
    
    var winnerName string
    var winnerSalary int
    if productivity1 >= productivity2 {
        winnerName = name1
        winnerSalary = salary1
    } else {
        winnerName = name2
        winnerSalary = salary2
    }
    bonus := winnerSalary * 15 / 100
    fmt.Println(winnerName)
    fmt.Println("Bonus: $" + strconv.Itoa(bonus))
    

    The code uses the fmt and strconv packages.

    The details worth knowing

    • Scope. A name declared with := or var inside { } exists only until the closing brace. If the branches said winnerName := name1, they would declare new variables that vanish at the brace, and the later fmt.Println(winnerName) would fail with undefined: winnerName. Each branch would also report winnerName declared and not used. That is why the lesson declares the variables outside and uses plain = inside.
    • var with a type and no value. var winnerSalary int gives 0 and var winnerName string gives the empty string. Go has no uninitialised variables.
    • No parentheses, required braces. The condition is written without parentheses, and the braces are mandatory even for one statement. The else must start on the same line as the }, otherwise the code does not compile.
    • The condition needs a bool. Go does not treat numbers as true or false, so if productivity1 - productivity2 { is a compile error. You have to write the comparison.

    Complexity

    The program does a fixed number of operations whatever the values are, so time and extra space are both O(1).

    Edge cases and common mistakes

    • Ties. With productivity1 := 100 and productivity2 := 100, the condition productivity1 >= productivity2 is true, so employee 1 wins. Using > would give the tie to employee 2 and break the rule.
    • Mixing the pair. Taking the name from one employee and the salary from the other prints the wrong bonus for the winner. Keep both assignments in the same branch.
    • Calculating inside the branches. It works, but repeats lines. The lesson's structure keeps the formula in one place.
    • Comparing salaries instead of productivity. In the starting data the higher-paid employee also wins, so the mistake would go unnoticed there. Try data where the more productive employee earns less.

    Testing

    • Productivities 83 and 133 with salaries 135000 and 335000 print Mary Jane and Bonus: $50250.
    • Productivities 150 and 90 with the same salaries print John Doe and Bonus: $20250.
    • Productivities 133 and 83 with salaries 100000 and 500000 print John Doe and Bonus: $15000: the more productive employee wins even though the other one earns more.
    • Productivities 100 and 100 print John Doe, the first employee.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free