Hi,
I'm practicing a basic algorithm from Freecodecamp.com.
The instruction is below.
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
For the purpose of this exercise, you should also capitalize connecting words like "the" and "of".
And this is my code.
const titleCase = str => {
let result = str.split(' ');
result = result.map(t => {
t = t.toLowerCase();
t[0] = t[0].toUpperCase();
return t;
});
return result.join(' ');
};
console.log(titleCase("I'm a little tea pot"));
Result
i'm a little tea pot
This is what I thought.
- Extract a whole string into pieces by whitespace.
- Make it lowercase.
- Make only first letter uppercase.
- return the result.
However, a problem is line:5 doesn't affect the result. The first letter doesn't change. Am I missing something, right? It seems I misunderstand some concepts about map method. Could you give some advice to fix that?
Thanks in advance.