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
- Split the name at the space. Store the result in
words, declared withletand the typestring[]. - Join the two initials. Add
words[0][0]andwords[1][0]with+. Put the sum in parentheses, calltoUpperCase()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]isundefined, and readingwords[1][0]fails with aTypeError. 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".
