Last active
May 23, 2018 03:31
-
-
Save denismcdonald/dbcf4d30aa10b55a7ef5fc22c89cd3e2 to your computer and use it in GitHub Desktop.
Capitalise the first letter of each word in a string (and otherwise convert to lower case) (JavaScript)
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) { | |
var splitString = str.split(" "); | |
var newArray = []; | |
for (var i = 0; i < splitString.length; i++) { | |
var splitWord = splitString[i].split(""); | |
splitWord[0] = splitWord[0].toUpperCase(); | |
for (var j = 1; j < splitWord.length; j++) { | |
splitWord[j] = splitWord[j].toLowerCase(); | |
} | |
splitWord = splitWord.join(""); | |
newArray.push(splitWord); | |
} | |
return newArray.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