Start learning free

LESSON 158 OF 404 · LOOPING OVER ARRAY INDICES

Employee of the Year V4 - Project in Go

Practice

A whole team is competing. Use a for loop over the slices to find the most productive employee, then print their 15% bonus.

You start with:

names := []string{"John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"}
productivities := []int{83, 133, 97, 150, 120}
salaries := []int{135000, 335000, 210000, 250000, 180000}

Build:

Alice Chen
Bonus: $37500

Rules:

  • Index i in all three slices refers to the same employee.
  • The winner is whichever employee has the highest productivity. If two 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.

Start with bestIndex := 0 and loop over the other indices with for i := 1; i < len(names); i++. Inside the loop, update the best index only when productivities[i] > productivities[bestIndex]: a strict comparison keeps the first employee on a tie.

Dividing two whole numbers (int) drops the fraction, so salaries[bestIndex] * 15 / 100 is already rounded down: bonus := salaries[bestIndex] * 15 / 100

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

names := []string{"John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"}
productivities := []int{83, 133, 97, 150, 120}
salaries := []int{135000, 335000, 210000, 250000, 180000}

bestIndex := 0
for i := 1; i < len(names); i++ {
    if productivities[i] > productivities[bestIndex] {
        bestIndex = i
    }
}
bonus := salaries[bestIndex] * 15 / 100
fmt.Println(names[bestIndex])
fmt.Println("Bonus: $" + strconv.Itoa(bonus))

STEP 1 OF 6
    Ctrl + Enter to check

    Employee Of The Year V4 in Go

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