Created
December 10, 2015 01:07
-
-
Save joePichardo/e91081aa8fa9eddad30b to your computer and use it in GitHub Desktop.
Capitalized first letter of each word, the rest are lower case.
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
// Bonfire: Title Case a Sentence | |
// Author: @joepichardo | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-title-case-a-sentence | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function titleCase(str) { | |
str = str.toLowerCase(); | |
//make array for each word | |
var stringArray = str.split(" "); | |
for(var i = 0; i < stringArray.length; i++){ | |
//store the first character of each word and make it UpperCase | |
var stringUpper = stringArray[i].charAt(0).toUpperCase(); | |
//we added the UpperCase letter first then the other | |
//characters are added in the for loop | |
for(var j = 1; j <= stringArray[i].length; j ++){ | |
stringUpper = stringUpper + stringArray[i].charAt(j); | |
} | |
//add corrected string to the array to | |
//join the sentence back together | |
stringArray[i] = stringUpper; | |
} | |
str = stringArray.join(" "); | |
return str; | |
} | |
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