Problems › JavaScript › warmup
Initials for an avatar
The user menu shows initials when someone has no profile picture.
- Take the first letter of each space-separated word, uppercased, joined by dots, with a trailing dot.
- Collapse repeated spaces, and ignore leading and trailing ones.
- An empty or blank name gives an empty string.
initials(fullName: string) → string
Where you start
function initials(fullName) {
}
Worked examples
| Call | Result |
|---|---|
initials("ada lovelace") | "A.L." |
initials("Grace Brewster Hopper") | "G.B.H." |
initials(" linus ") | "L." |
initials("") | "" |
Hint
Split on whitespace, drop the empty pieces, then take index 0 of each.
Reference solution in JavaScript
function initials(fullName) {
let out = '';
for (const w of fullName.split(/\s+/)) {
if (w) out += w[0].toUpperCase() + '.';
}
return out;
}