Created
December 19, 2015 14:56
-
-
Save rurtubia/7684dbfa31f3c9a0617b to your computer and use it in GitHub Desktop.
My solution to FreeCodeCamp bonfire 6: Title Case a Sentence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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