Exercise 8 of 10 · Functions
Board Game Leaderboard
What you will make
A leaderboard for a board game night that finds the top score with a helper function and marks the winning player.
The one new idea: Writing and calling a function with a parameter and a return value: func highestScore(scores []int) int
Pulling a calculation out into a named function is how Go programs stay readable as they grow — this is the first exercise where you write a function instead of just calling ones from the standard library.
Go straight to the code ↓A Go function signature like func highestScore(scores []int) int says three things: the function's name, the type of value it accepts (a slice of ints), and the type of value it hands back (a single int). Inside, return best is what sends that value out to whoever called the function. Writing your own small functions like this — instead of inlining every calculation directly in main — is what keeps larger programs readable, because main can just say best := highestScore(scores) and trust that the details are handled elsewhere.
Worked example
Here's the same leaderboard function used on a two-player game:
package main
import "fmt"
func highestScore(scores []int) int {
best := scores[0]
for i := 1; i < len(scores); i++ {
if scores[i] > best {
best = scores[i]
}
}
return best
}
func main() {
players := []string{"Tao", "Wren"}
scores := []int{14, 20}
best := highestScore(scores)
fmt.Println("Board game leaderboard:")
for i := 0; i < len(players); i++ {
marker := ""
if scores[i] == best {
marker = " <- WINNER"
}
fmt.Printf("%s: %d%s\n", players[i], scores[i], marker)
}
}Board game leaderboard:
Tao: 14
Wren: 20 <- WINNERhighestScore returns 20, and the loop in main marks whichever player's score equals that returned value.
Your turn
The starter program's highestScore function is meant to track the biggest score in the slice, and main is meant to mark whichever player matches that score — but one comparison in each place is backwards. Fix both marked lines.
If something goes wrong
If highestScore returns the smallest score instead of the largest, check the comparison inside its loop — it should replace best only when it finds something bigger, using >. If no player (or every player) ends up marked WINNER, look at the comparison in main's loop — it should match scores that are equal to best, using ==, not scores that differ from it.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Using < when tracking a maximum, or > when tracking a minimum.
- These two comparisons produce opposite results — a 'keep the biggest' loop needs to replace best only when it finds something bigger.
- Using != when the goal is to detect a match.
- != is true for every non-matching value, which is the opposite of what you want when flagging the single item that does match.
- Forgetting a function needs a return type when it produces a value.
- func highestScore(scores []int) int declares both the parameter type and the return type; leaving off int would be a compile error since the function body does return a value.
- Starting the comparison loop at index 0 instead of 1 after already reading scores[0].
- best is initialized from scores[0], so comparing it against itself at i=0 is redundant — starting at i=1 avoids the wasted comparison, though starting at 0 wouldn't be wrong, just less tidy.
Want a blank editor instead? Open the Go playground.