freeCodeCamp:Title Case a Sentence

用js操作保证字符串的每个单词首字母都大写,其余部分小写。

function titleCase(str) {
  var arr = str.split(" ");
  var newArray = [];
  for (var i = 0; i < arr.length; i++) {
    var newStr = arr[i].slice(0,1).toUpperCase() + arr[i].slice(1).toLowerCase();
    newArray.push(newStr);
  }
  return newArray.join(" ");
}

titleCase("I'm a little tea pot");

依然是针对split的练习,还有slice()的使用,

再就是toLowerCase()和toUpperCase()的用法,要搞清前两者和toLocaleLowerCase()、toLocaleUpperCase()的区别

 

posted @ 2016-10-26 22:45  酸菜有毒  阅读(658)  评论(0编辑  收藏  举报