Skip to content

Instantly share code, notes, and snippets.

@bflannery
Created December 20, 2016 17:16
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 bflannery/911ba8e00733129f105e508f569c2cbb to your computer and use it in GitHub Desktop.
Save bflannery/911ba8e00733129f105e508f569c2cbb to your computer and use it in GitHub Desktop.
Return the first letter of each word capitalized
// 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".
function titleCase(str) {
str = str.toLowerCase().split(' ');
for(var i=0; i < str.length; i++) {
str[i]= str[i].charAt(0).toUpperCase() + str[i].slice(1);
}
return str.join(' ');
}
console.log(titleCase("I'm a little tea pot"));
console.log(titleCase("sHoRt AnD sToUt"));
console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment