-
-
Save khaduch/8b2fa77d95fe8ab15af2f95b8b227e82 to your computer and use it in GitHub Desktop.
The freeCodeCamp Title Case challenge - my solution(s)
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
// 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