TypeScript bootcamp · Lab 25

Title case

easyStrings10 minLesson: Strings

Read the question, write TypeScript on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read a line; print title case (first letter of each word upper, rest lower).

Input. One line.

Output. Title-cased text.

Examples

Example 1
Input
hello world
Output
Hello World
Hint
  1. Split on spaces; map word => word[0].toUpperCase() + word.slice(1).toLowerCase()
Show correct code

Peek only after you have tried. You can still Check your own version.

const line = readLine().trim();
console.log(line.split(/\s+/).map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join(' '));
main.tstsc · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.