Start learning free

LESSON 243 OF 285 · HELPER FUNCTIONS

Letter Grades in Go: A Helper and a Loop

Practice Read the full explanation

Given an array of integers scores, return the letter grade of every score, in the same order as the scores.

Each score gets one letter:

  • 90 to 100: "A"
  • 80 to 89: "B"
  • 70 to 79: "C"
  • 60 to 69: "D"
  • 0 to 59: "F"

The work is split between two functions. The first one is a helper: it gets one score and returns its letter. The second one, the function that is graded, goes over the scores and collects the helper's letter for each of them.

 

Example 1:

Input: scores = [95, 82, 71]
Output: ["A", "B", "C"]
Explanation: 95 is in the A range (90 to 100), 82 is in the B range and 71 is in the C range.

Example 2:

Input: scores = [65, 40]
Output: ["D", "F"]
Explanation: 65 is between 60 and 69, so it gets a D. 40 is below 60, so it gets an F.

Example 3:

Input: scores = [100, 90, 89]
Output: ["A", "A", "B"]
Explanation: 100 and 90 are both in the A range: 90 is its lowest score. 89 is one point short of it, so it gets a B.

 

Constraints:

  • scores has at least one score.
  • Every score is an integer from 0 to 100.
  • The result has one letter per score, in the same order as scores.
STEP 1 OF 9
    Ctrl + Enter to check

    GO · HELPER FUNCTIONS

    Letter Grades in Go, explained

    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

    1. Return "A" when score >= 90.
    2. Return "B" when score >= 80. Scores of 90 and up already returned, so this means 80 to 89.
    3. Return "C" when score >= 70.
    4. Return "D" when score >= 60.
    5. Return "F" after the last if. Only scores below 60 reach it.
    6. In letterGrades, create grades as an empty slice of strings with :=.
    7. Loop over scores with range, keeping each value in score and dropping the index with _.
    8. Append letterGrade(score) to grades and assign the result back to grades.
    9. Return grades after 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:

    • append returns the slice. It may need a bigger array and hand back a new slice header, so the result must be assigned: grades = append(grades, ...). Calling append and ignoring the result does not compile.
    • []string{} is an empty slice, ready to use. A slice declared as var grades []string is nil, and append works 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 a string. Single quotes make a rune, a different type that would not match the return type.
    • The helper's score is a separate variable from the loop's score. 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:

    scorefirst test that passesletterGrade(score)
    95score >= 90A
    82score >= 80B
    71score >= 70C

    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 >= 60 came 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 append per 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.

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