1function titleCase(str) {
2 return str
3 .split(' ')
4 .map((word) => word[0].toUpperCase() + word.slice(1).toLowerCase())
5 .join(' ');
6}
7console.log(titleCase("I'm a little tea pot")); // I'm A Little tea Pot
8
1function titleCase(sentence) {
2 let sentence = string.toLowerCase().split(" ");
3 for (let i = 0; i < sentence.length; i++) {
4 sentence[i] = sentence[i][0].toUpperCase() + sentence[i].slice(1);
5 }
6
7 return sentence.join(" ");
8}
9
1function toTitles(s){ return s.replace(/\w\S*/g, function(t) { return t.charAt(0).toUpperCase() + t.substr(1).toLowerCase(); }); }
2var str = toTitles('abraham lincoln'); // Abraham Lincoln
1function toTitleCase(str) {
2 return str.replace(
3 /\w\S*/g,
4 function(txt) {
5 return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
6 }
7 );
8}
9// example
10toTitleCase("the pains and gains of self study");
11// "The Pains And Gains Of Self Study"