Skip to content

Instantly share code, notes, and snippets.

@denismcdonald
Last active May 23, 2018 03:31
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 denismcdonald/dbcf4d30aa10b55a7ef5fc22c89cd3e2 to your computer and use it in GitHub Desktop.
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)
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