Tidy up the capital letters of a line of text.
What to build
A program that makes every word start with a capital, and only with a capital.
Requirements
- Read a line of words.
- In each word, make the first character upper case and all the others lower case.
- Print the words separated by single spaces.
Input
One line of words separated by single spaces.
Output
One line.
Constraints
The line has from 1 to 50 words. Words contain letters and digits.
Example
Input:
the QUICK brown FOX
Output:
The Quick Brown Fox
Why it works
Lower-casing first means that only one rule is needed, whatever the input looked like. slice(1) gives the part of the word after its first character, and for a first character that is a digit, toUpperCase simply changes nothing.
Examples
Example 1 · Input
hello world
Expected output
Hello World
Example 2 · Input
the QUICK brown FOX
Expected output
The Quick Brown Fox
Hints
Hint 1
Change the whole word to lower case first.
Hint 2
word[0].toUpperCase() + word.slice(1) capitalises a word.
Hint 3
Add a space after each word, and use trim() on the finished line.
Solution
Show one correct solution (try it yourself first)
const words = prompt().split(" ");
let result = "";
for (const word of words) {
const lower = word.toLowerCase();
result += lower[0].toUpperCase() + lower.slice(1) + " ";
}
console.log(result.trim());