rurtubia
12/19/2015 - 2:56 PM

My solution to FreeCodeCamp bonfire 6: Title Case a Sentence

My solution to FreeCodeCamp bonfire 6: Title Case a Sentence

function titleCase(str) {
  //split the string into an array of strings
  var tempArray = str.split(' ');
  
  //for each string in the array...
  for(var i=0;i<tempArray.length;i++)
    {
      //change all words to lower case
      tempArray[i] = tempArray[i].toLowerCase();
      
      //replace the first character in the string for the same character in upper case
      tempArray[i] = tempArray[i].replace(tempArray[i].charAt(0), tempArray[i].charAt(0).toUpperCase()); 
      
    }
  //join the array into a string again
  str = tempArray.join(' ');
  
  //return the string
  return str;
}

titleCase("little tea pot");