Javascript String Capitalize words to make title case

Description

Javascript String Capitalize words to make title case

function titleCase(str) {
  
  str = str.toLowerCase(str);/*from  w  w w. j  a v  a2 s .  c om*/
  
  var arr = str.split(" ");
  var i = 0;
  
  while (i < arr.length) {
    console.log(arr[i].charAt(0));
    arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
    i++;
  }
  
  str = arr.join(' ');

  return str;
}

console.log(titleCase("I'm a little tea pot"));
console.log(titleCase("sHoRt AnD sToUt"));
console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));



PreviousNext

Related