Overview
Clean up and slice text with string methods to build the two-letter avatar label you see in chat apps.
- length counts characters
- trim, toUpperCase and slice
- includes and indexOf search text
- Methods return a new string; the original never changes
Short Course
String methods never change the original text: they return a new string.
The trap is calling name.toUpperCase() and forgetting to keep the result.
const name = " Ada Lovelace ".trim(); // "Ada Lovelace"
name.length; // 12
name.toUpperCase(); // "ADA LOVELACE" (name is unchanged)
name.slice(0, 3); // "Ada"
name.includes("Love"); // true
name.indexOf(" "); // 3
Task
Write initials(fullName) that returns the uppercase first letters of the
first and last name, ignoring extra spaces, e.g. initials("ada lovelace")
returns AL.
Test Suites
Pending
Next
Output