Start learning free

LESSON 79 OF 285 · STRINGS - CHALLENGES

Initials in JavaScript: 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

    JAVASCRIPT · STRINGS - CHALLENGES

    Initials in JavaScript, 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.

    "grace hopper" gives "GH", because the initials are upper case even when the name is written in lower case. "Ada Lovelace" gives "AL". A name can be a single letter: "a b" gives "AB".

    Approach

    First split the full name at the space into two words. "grace hopper".split(" ") is ["grace", "hopper"]. Then take the first character of each word and upper-case the pair.

    words[0] is the first word, and a string can be indexed too, so words[0][0] is its first character: the first index picks the word, the second picks the character. Joining the two initials with + gives a two-character string, and toUpperCase() returns it in upper case. The sum goes in parentheses so the method applies to both letters, not only the second one.

    Step by step

    1. Split the name at the space into an array called words. With exactly one space, there are exactly two words.
    2. Join the initials words[0][0] and words[1][0], put them in parentheses, call toUpperCase() and return the result.

    Following the indexes

    For "grace hopper", split(" ") gives ["grace", "hopper"]. Then words[0][0] is "g" and words[1][0] is "h". Their sum is "gh", and toUpperCase() turns it into "GH". For "a b" the split gives ["a", "b"], and each word is a single character, whose first character is itself.

    The solution in JavaScript

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

    On the examples it returns:

    "grace hopper" -> "GH"
    "Ada Lovelace" -> "AL"
    "a b"          -> "AB"
    

    Why exactly one space matters

    The lesson guarantees one space between the names. With two spaces, "a b".split(" ") gives ["a", "", "b"], three items with an empty string in the middle, and words[1][0] would then be undefined. A single space gives exactly two words, so indexes 0 and 1 are the two names.

    The method toUpperCase() returns a new string, because strings don't change. Calling it without using the result does nothing useful.

    Thinking about the problem

    Two sub-problems come out of the statement: find the two names, then take the first letter of each. Splitting at the space solves the first. A letter of a word is read with an index, so the second is words[0][0] and words[1][0]. The upper-case conversion is applied once, at the end, to the joined pair. That is shorter than converting each letter separately, and the result is the same.

    Print words after the split to see what the array holds before indexing into it. If it has two items, the rest of the code only reads the first character of each.

    Edge cases and common mistakes

    • Upper-casing only one letter. Without the parentheses, words[0][0] + words[1][0].toUpperCase() upper-cases just the second initial, so "grace hopper" would give "gH".
    • Forgetting the second index. words[0] is the whole first name, not its first letter.
    • Letters that are already upper case. toUpperCase() leaves them unchanged, so "Ada Lovelace" works with the same code.

    Testing

    • "ALAN TURING" gives "AT".
    • "linus Torvalds" gives "LT": a lower-case first name and an upper-case last name are handled by the same call.
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free