This problem calls for an algorithm to capitalize the first letter of each word in a string. Having to manually update several words in a given body of text is a major tedious nuisance, and already this algorithm can be handy for updating cases within a large body of work.
Below is my solution:
function titleCase(str) {
var LowerCased = str.toLowerCase();
var SplitString = LowerCased.split(‘ ‘);
for (var i = 0; i < SplitString.length; i++) {
SplitString[i] = SplitString[i].split(”);
SplitString[i][0] = SplitString[i][0].toUpperCase();
SplitString[i] = SplitString[i].join(”);
var Rejoined = SplitString.join(‘ ‘);
}
return Rejoined;
}
titleCase(“I’m a little tea pot”);
Breakdown:
var LowerCased = str.toLowerCase();
We want to make sure that all the strings are lower case, because the ultimate purpose of this algorithm is to ensure that consistently the first letter of each word is capitalized, while the remaining letters remain lower case. For example, we do not want “bAd foRMATing” to end up as “BAd FoRMAting”