Start learning free

LESSON 243 OF 289 · HELPER FUNCTIONS

Letter Grades in TypeScript: A Helper Function 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

    TYPESCRIPT · HELPER FUNCTIONS

    Letter Grades in TypeScript, explained

    Problem overview

    The method letterGrades receives an array of scores, each an integer from 0 to 100, and returns an array of letters 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".

    The work is split in two. The helper letterGrade turns one score into one letter, and letterGrades loops over the array and collects the helper's answers. 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, and 89 is one point short.

    Approach: one decision, many times

    Converting a single score is a decision with five outcomes. Converting an array is that same decision repeated for every item. Writing the decision as its own function keeps each part small: the helper only thinks about one score, and the loop only thinks about walking the array.

    Inside the helper, the checks go from the top grade down. A score that is at least 90 returns "A" at once. Any score that gets past the first if is below 90, so the next check only needs "at least 80", and so on. Each test can ignore the ranges above it, because a return already ended the function for those scores.

    Step by step

    1. Check score >= 90 and return "A".
    2. Check score >= 80 and return "B". Here the score is known to be below 90, so this means 80 to 89.
    3. Check score >= 70 and return "C".
    4. Check score >= 60 and return "D".
    5. Return "F" after the last if. Only scores below 60 reach this line, so it needs neither a test nor an else.
    6. Start grades as an empty string[] in letterGrades. The letters are added one at a time.
    7. Loop with for (const score of scores). It runs once per score, in order.
    8. Push the helper's answer with grades.push(this.letterGrade(score)). The helper's parameter is also called score, but each function has its own variable; the call hands the helper the loop's current value.
    9. Return grades after the loop, so every score is processed first.

    The solution in TypeScript

    class Solution {
        letterGrade(score: number): string {
            if (score >= 90) {
                return "A";
            }
            if (score >= 80) {
                return "B";
            }
            if (score >= 70) {
                return "C";
            }
            if (score >= 60) {
                return "D";
            }
            return "F";
        }
    
        letterGrades(scores: number[]): string[] {
            let grades: string[] = [];
            for (const score of scores) {
                grades.push(this.letterGrade(score));
            }
            return grades;
        }
    }
    

    Language details worth knowing:

    • this. before the helper's name. Methods of a class call each other through this. Without it the compiler reports that it can't find the name, and the call fails at run time.
    • for...of versus for...in. for (const score of scores) gives the values. for...in on an array gives the indices as strings, which is not what this loop needs.
    • const in the loop head. Each turn creates a fresh score that is never reassigned, so const is correct there.
    • string[]. The annotation says grades holds strings. It is removed when the code is compiled to JavaScript, so it checks nothing at run time.
    • Strings are written with quotes. "A" is a string; a bare A would be read as a variable name.

    Following the helper by hand

    It helps to ask where each score leaves the helper. A score of 95 is stopped by the first test and gets "A". A score of 82 fails the first test, passes the second and gets "B". A score of 40 fails all four tests and reaches the last line, which gives "F". Running the helper through letterGrades on [100, 90, 89] produces ["A", "A", "B"]: 100 and 90 both stop at the first test, while 89 falls through to the second.

    Because every score leaves through exactly one return, the helper never returns two letters. The final return "F" also guarantees that every path ends in a value, which a function declared to return string needs. If that last line were missing, a score below 60 would make the function return undefined instead of a letter, and the lesson's compiler settings would not warn about it.

    The same pattern works for any set of ranges: put the strictest test first, give each test only its lower limit, and finish with a default.

    Edge cases and common mistakes

    • Boundaries. 90, 80, 70 and 60 each belong to the higher grade. letterGrades([80, 79, 70, 69, 60, 59, 0]) returns ["B", "C", "C", "D", "D", "F", "F"]. Using > 90 instead of >= 90 would give a 90 a "B".
    • Checking the lowest range first. If the first test were score >= 60, a score of 95 would return "D". The order from highest to lowest is what makes the short tests correct.
    • Returning inside the loop. A return there sends back the first letter only, not the array.
    • Mixing up the two functions. letterGrade takes a number and returns a string; letterGrades takes an array and returns an array.

    Testing

    • [95, 82, 71] gives ["A", "B", "C"].
    • [65, 40] gives ["D", "F"].
    • [100, 90, 89] gives ["A", "A", "B"].
    • [80, 79, 70, 69, 60, 59, 0] gives ["B", "C", "C", "D", "D", "F", "F"].
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free