Skip to content

Instantly share code, notes, and snippets.

@khaduch
Created November 15, 2017 15:27
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 khaduch/8b2fa77d95fe8ab15af2f95b8b227e82 to your computer and use it in GitHub Desktop.
Save khaduch/8b2fa77d95fe8ab15af2f95b8b227e82 to your computer and use it in GitHub Desktop.
The freeCodeCamp Title Case challenge - my solution(s)
// revised to use .map and some ES6
function titleCase(str){
return str
.toLowerCase()
.split(' ')
.map( w => w[0].toUpperCase() + w.slice(1) )
.join(' ');
}
function ORIGINAL_titleCase(str) {
var splitStr = str.split(' '),
splitLen = splitStr.length,
i;
for (i = 0; i < splitLen; i++) {
splitStr[i] = splitStr[i].toLowerCase().replace(/^\w/, splitStr[i].charAt(0).toUpperCase());
}
return splitStr.join(' ');
}
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