Problem overview
Given a slice of integer scores, return the letter grade of every score, in the same order. The ranges are 90 to 100 for "A", 80 to 89 for "B", 70 to 79 for "C", 60 to 69 for "D", and 0 to 59 for "F".
Two functions share the work. The helper letterGrade turns one score into its letter. The graded function letterGrades goes over the slice and collects the helper's answer for each score.
For [95, 82, 71] the result is [A B C], and for [100, 90, 89] it is [A A B]: 90 is the lowest A, so 89 falls one point short and gets a B.
Approach: check the highest range first
The helper is a chain of checks. Start at the top: a score of at least 90 is an A. Because return ends the function, every later check only ever sees scores below 90, so the next test can be just "at least 80". Each check only needs its lower bound, and the chain needs no upper bounds and no else. The last line returns "F" for whatever is left.
The second function is a plain accumulate-in-a-loop pattern: start with an empty slice, visit each score, append that score's letter, and return the slice at the end.
Step by step
- Return
"A"whenscore >= 90. - Return
"B"whenscore >= 80. Scores of 90 and up already returned, so this means 80 to 89. - Return
"C"whenscore >= 70. - Return
"D"whenscore >= 60. - Return
"F"after the lastif. Only scores below 60 reach it. - In
letterGrades, creategradesas an empty slice of strings with:=. - Loop over
scoreswithrange, keeping each value inscoreand dropping the index with_. - Append
letterGrade(score)togradesand assign the result back togrades. - Return
gradesafter the loop. Returning inside the loop would send back only the first letter.
The solution in Go
func letterGrade(score int) string {
if score >= 90 {
return "A"
}
if score >= 80 {
return "B"
}
if score >= 70 {
return "C"
}
if score >= 60 {
return "D"
}
return "F"
}
func letterGrades(scores []int) []string {
grades := []string{}
for _, score := range scores {
grades = append(grades, letterGrade(score))
}
return grades
}
Go details worth knowing:
appendreturns the slice. It may need a bigger array and hand back a new slice header, so the result must be assigned:grades = append(grades, ...). Callingappendand ignoring the result does not compile.[]string{}is an empty slice, ready to use. A slice declared asvar grades []stringisnil, andappendworks on it too. Here the lesson's constraints guarantee at least one score, so both would behave the same for the tests.- Strings use double quotes.
"A"is astring. Single quotes make arune, a different type that would not match the return type. - The helper's
scoreis a separate variable from the loop'sscore. The call hands the helper a copy of the loop's value.
Following the example through the code
For scores = [95, 82, 71], the loop calls the helper three times, once per score. Each call walks down the chain and stops at the first test that passes:
| score | first test that passes | letterGrade(score) |
|---|---|---|
| 95 | score >= 90 | A |
| 82 | score >= 80 | B |
| 71 | score >= 70 | C |
The slice grades grows from empty to [A], then [A B], then [A B C], and that last slice is what the function returns.
An equivalent chain with switch
Go also lets a switch without a value act as a chain of conditions. This version gives the same letters, for example 95 gives A, 65 gives D and 40 gives F:
func letterGrade(score int) string {
switch {
case score >= 90:
return "A"
case score >= 80:
return "B"
case score >= 70:
return "C"
case score >= 60:
return "D"
}
return "F"
}
The cases are tried from the top, and the first one that is true runs, so the order matters here exactly as it does with the if chain. Either form is fine; the lesson uses if.
Why split it into two functions
The helper knows nothing about slices, and letterGrades knows nothing about grade ranges. If the school changes where a B starts, only letterGrade changes. If the input becomes a different kind of collection, only the loop changes. Each function is also easy to check alone: letterGrade(89) is "B" and letterGrade(90) is "A", and once the helper is right the loop has just one thing left to get right, which is collecting the answers in order.
Edge cases and common mistakes
- Boundaries. The scores 59, 60, 69, 70, 79, 80 and 0 give
[F D D C C B F]. Using>instead of>=would push every boundary score down a grade. - Checking in the wrong order. If the test
score >= 60came first, every passing score would get a D. With a chain of lower bounds, the order from highest to lowest is what makes it correct. - Losing the order. One
appendper score, in a single pass, keeps the letters aligned with the scores. - Forgetting to assign the result of
append. The compiler reports that the value is not used.
Testing
[95, 82, 71]gives[A B C].[65, 40]gives[D F].[100, 90, 89]gives[A A B].[59, 60, 69, 70, 79, 80, 0]gives[F D D C C B F].
These are printed with fmt.Println, which shows a slice of strings in square brackets without quotes.
