Start learning free

LESSON 81 OF 289 · STRINGS - CHALLENGES

Initials in TypeScript: split() and toUpperCase()

Practice Read the full explanation

Given a person's full name fullName, made of a first name and a last name, return their initials: the first letter of each name, in upper case.

The two names are separated by a single space. The initials are always upper case, even when the name is written in lower case.

 

Example 1:

Input: fullName = "grace hopper"
Output: "GH"
Explanation: The first name starts with g and the last name with h. In upper case, they give "GH".

Example 2:

Input: fullName = "Ada Lovelace"
Output: "AL"
Explanation: A and L are already upper case, so they stay as they are.

Example 3:

Input: fullName = "a b"
Output: "AB"
Explanation: A name can be a single letter: that letter is its initial.

 

Constraints:

  • fullName has exactly two words, the first name and the last name, separated by one space.
  • Both names are made of English letters only, lower case or upper case.
STEP 1 OF 2
    Ctrl + Enter to check

    TYPESCRIPT · STRINGS - CHALLENGES

    Initials in TypeScript, explained

    Problem overview

    The method initials receives a full name, fullName, made of a first name and a last name separated by one space. It returns the first letter of each name, in upper case. For "grace hopper" the answer is "GH": the letters are g and h, and the result is upper case even though the name was typed in lower case. For "Ada Lovelace" the answer is "AL", and for "a b" it is "AB".

    Approach

    There are three small tasks: separate the two names, take the first letter of each, and change the case. The split method cuts a string at every occurrence of a separator and returns an array of the pieces. Since the name has exactly one space, fullName.split(" ") returns an array of exactly two words. For "grace hopper" that is ["grace", "hopper"].

    Then words[0] is the first name and words[0][0] is its first character: the first index picks the word, the second picks the character inside it. The method toUpperCase() returns a copy of a string with every letter in upper case.

    Step by step

    1. Split the name at the space. Store the result in words, declared with let and the type string[].
    2. Join the two initials. Add words[0][0] and words[1][0] with +. Put the sum in parentheses, call toUpperCase() on it and return the result.

    The solution in TypeScript

    class Solution {
        initials(fullName: string): string {
            let words: string[] = fullName.split(" ");
            return (words[0][0] + words[1][0]).toUpperCase();
        }
    }
    

    The parentheses matter. A method call binds tighter than +, so without them toUpperCase() would apply only to the last piece. For two lower-case initials "g" and "h", "g" + "h".toUpperCase() gives "gH", while ("g" + "h").toUpperCase() gives "GH".

    Because toUpperCase() returns a new string, the name stored in fullName stays as it was. Upper-casing a letter that is already upper case changes nothing, which is why "Ada Lovelace" needs no special handling.

    Tracing the example

    For "grace hopper", words is ["grace", "hopper"]. Then words[0][0] is "g" and words[1][0] is "h". Their sum is "gh", and toUpperCase() turns it into "GH". A name made of single letters, "a b", splits into ["a", "b"] and gives "AB".

    Other ways to write it

    Mapping over the words gives the same result and also works with more than two names: fullName.split(" ").map(w => w[0]).join("").toUpperCase() returns "GH" for "grace hopper". The lesson's version spells out the two indexes, which makes the two-level indexing visible.

    The method works with names of any length, because only the first character of each word is read.

    Edge cases and common mistakes

    • Forgetting the parentheses. Only the second initial would be upper case, giving "gH".
    • Using one index. words[0] is the whole first name, not its first letter. Two indexes are needed.
    • Splitting on the wrong separator. split(",") would leave the name in one piece. The lesson separates names with a space, so the separator is " ".
    • Extra spaces. The lesson guarantees exactly one. With two spaces, as in "grace hopper", the split gives ["grace", "", "hopper"], and the second word would be an empty string.
    • A single name. For "Cher" the array has one word, words[1] is undefined, and reading words[1][0] fails with a TypeError. The lesson's input always has two names.

    Testing

    • "grace hopper" gives "GH".
    • "Ada Lovelace" gives "AL".
    • "a b" gives "AB".
    • "mary-ann Smith" gives "MS".
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free