Skip to content

Instantly share code, notes, and snippets.

  • Save anonymous/a00e7293b71303bc7968 to your computer and use it in GitHub Desktop.
Save anonymous/a00e7293b71303bc7968 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/goodbedford 's solution for Bonfire: Title Case a Sentence
// Bonfire: Title Case a Sentence
// Author: @goodbedford
// Challenge: http://www.freecodecamp.com/challenges/bonfire-title-case-a-sentence?solution=function%20titleCase(str)%20%7B%0A%20%20%0A%20%20var%20strArr%20%3D%20str.split(%22%20%22)%3B%0A%20%20var%20tempArr%20%3D%20%5B%5D%3B%0A%20%20%0A%20%20strArr.forEach(function(word)%7B%0A%20%20%20%20var%20firstChar%20%3D%20word%5B0%5D.toUpperCase()%3B%0A%20%20%20%20word%20%3D%20word.toLowerCase()%3B%0A%20%20%20%20word%20%3D%20firstChar%20%2B%20word.slice(1)%3B%0A%20%20%20%20tempArr.push(word)%3B%0A%20%20%7D)%3B%0A%20%20%0A%20%20return%20tempArr.join(%22%20%22)%3B%0A%7D%0A%0AtitleCase(%22I%27m%20a%20little%20tea%20pot%22)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function titleCase(str) {
var strArr = str.split(" ");
var tempArr = [];
strArr.forEach(function(word){
var firstChar = word[0].toUpperCase();
word = word.toLowerCase();
word = firstChar + word.slice(1);
tempArr.push(word);
});
return tempArr.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