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
- Split the name at the space into an array called
words. With exactly one space, there are exactly two words. - Join the initials
words[0][0]andwords[1][0], put them in parentheses, calltoUpperCase()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.
