Skip to content

Instantly share code, notes, and snippets.

@SerhiiLihus
Last active November 30, 2015 09:02
Show Gist options
  • Save SerhiiLihus/f1829c645d9e71b1aa98 to your computer and use it in GitHub Desktop.
Save SerhiiLihus/f1829c645d9e71b1aa98 to your computer and use it in GitHub Desktop.
Title Case a Sentence
// Bonfire: Title Case a Sentence
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-title-case-a-sentence?solution=function%20titleCase(str)%20%7B%0A%20str%3Dstr.toLowerCase()%3B%0A%20var%20newString%20%3D%20str.charAt(0).toUpperCase()%3B%0A%20for(var%20i%20%3D%201%20%3B%20i%20%3C%20str.length%20%3B%20i%2B%2B%20)%0A%20%20%20%20%20if%20(%20str.charAt(i-1)%20%3D%3D%20%22%20%22)%20%0A%20%20%20%20%20%20%20%20%20newString%20%2B%3D%20%20%20%20%20str.charAt(i).toUpperCase()%3B%0A%20%20%20%20else%20%0A%20%20%20%20%20%20%20%20newString%20%2B%3D%20str.charAt(i)%3B%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20return%20newString%3B%0A%7D%0AtitleCase(%22I%27m%20a%20little%20tea%20pot%22)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function titleCase(str) {
str=str.toLowerCase();
var newString = str.charAt(0).toUpperCase();
for(var i = 1 ; i < str.length ; i++ )
if ( str.charAt(i-1) == " ")
newString += str.charAt(i).toUpperCase();
else
newString += str.charAt(i);
return newString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment