Skip to content

Instantly share code, notes, and snippets.

@waltzaround
Created September 29, 2015 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waltzaround/8db28418015484f5157d to your computer and use it in GitHub Desktop.
Save waltzaround/8db28418015484f5157d to your computer and use it in GitHub Desktop.
function titleCase(str) {
var newstr = str.split(" ");//split whatever input we have been given using the split object...
for(i=0;i<newstr.length;i++){ //as we cycle through each newly separated string in the array...
newstr[i] = newstr[i].charAt(0).toUpperCase() + newstr[i].substring(1).toLowerCase();
// we take the first character in the string and capitalize it, and each subsequent character in the separated word lowercase
}
newstr = newstr.join(" "); // now that we are done, join the strings from the array back together
return newstr // output finished result as we are done with the titleCase function
}
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