Skip to content

Instantly share code, notes, and snippets.

@rurtubia
Created December 19, 2015 14:56
Show Gist options
  • Save rurtubia/7684dbfa31f3c9a0617b to your computer and use it in GitHub Desktop.
Save rurtubia/7684dbfa31f3c9a0617b to your computer and use it in GitHub Desktop.
My solution to FreeCodeCamp bonfire 6: Title Case a Sentence
function titleCase(str) {
//split the string into an array of strings
var tempArray = str.split(' ');
//for each string in the array...
for(var i=0;i<tempArray.length;i++)
{
//change all words to lower case
tempArray[i] = tempArray[i].toLowerCase();
//replace the first character in the string for the same character in upper case
tempArray[i] = tempArray[i].replace(tempArray[i].charAt(0), tempArray[i].charAt(0).toUpperCase());
}
//join the array into a string again
str = tempArray.join(' ');
//return the string
return str;
}
titleCase("little tea pot");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment