Skip to content

Instantly share code, notes, and snippets.

@erictleung
Created November 21, 2015 08:57
Show Gist options
  • Save erictleung/9e35a0f309774fd4d80a to your computer and use it in GitHub Desktop.
Save erictleung/9e35a0f309774fd4d80a to your computer and use it in GitHub Desktop.
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lowercase.
function titleCase(str) {
var splitStr = str.split(" ");
var newStr = [];
for (i = 0; i < splitStr.length; i++) {
var temp = splitStr[i];
var fullTemp = temp[0].toUpperCase() + temp.substr(1, temp.length).toLowerCase();
newStr.push(fullTemp);
}
return newStr.join(" ");
}
titleCase("I'm a little tea pot");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment