美文网首页
Title Case a Sentence | Free Cod

Title Case a Sentence | Free Cod

作者: Marks | 来源:发表于2017-05-02 10:39 被阅读11次

确保字符串的每个单词首字母都大写,其余部分小写
titleCase("I'm a little tea pot") should return a string.
titleCase("I'm a little tea pot") should return "I'm A Little Tea Pot".
titleCase("sHoRt AnD sToUt") should return "Short And Stout".
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT") should return "Here Is My Handle Here Is My Spout".

//方法一:charAt() + slice
function titleCase(str) {
  var arr = str.toLowerCase().split(" ");
  for(var i=0;i<arr.length;i++){
   arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
  }
  return arr.join(" ");
}
titleCase("I'm a little tea pot");```

//方法二:charAt() + replace
function titleCase(str) {
var arr = str.toLowerCase().split(" ");
var result = arr.map(function(val){
return val.replace(val.charAt(0),val.charAt(0).toUpperCase());
});
return result.join(" ");
}

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

https://github.com/freeCodeCamp/freeCodeCamp/wiki/Algorithm-Title-Case-A-Sentence











相关文章

  • Title Case a Sentence | Free Cod

    确保字符串的每个单词首字母都大写,其余部分小写titleCase("I'm a little tea pot") ...

  • Title Case a Sentence

    句中单词首字母大写 确保字符串的每个单词首字母都大写,其余部分小写。 像'the'和'of'这样的连接符同理。

  • Title Case a Sentence

    确保字符串的每个单词首字母都大写,其余部分小写。 像'the'和'of'这样的连接符同理。

  • Title Case a Sentence -- Freecod

    确保字符串的每个单词首字母都大写,其余部分小写。像'the'和'of'这样的连接符同理。当你完成不了挑战的时候,记...

  • Title Case a Sentence(FCC practi

    句中单词首字母大写确保字符串的每个单词首字母都大写,其余部分小写。像'the'和'of'这样的连接符同理。当你完成...

  • JS-Title Case a Sentence

    Title Case a Sentence确保字符串的每个单词首字母都大写,其余部分小写。 问题描述是不是有时候会...

  • FCC 题目 Title Case a Sentence

    要求 确保字符串的每个单词首字母都大写,其余部分小写。 像'the'和'of'这样的连接符同理。 当你完成不了挑战...

  • FCC-JS-BAS-Title Case a Sentence

    句中单词首字母大写确保字符串的每个单词首字母都大写,其余部分小写。像'the'和'of'这样的连接符同理。当你完成...

  • Design & Coed 3: 找出最长单词

    句中单词首字母大写 Title Case a Sentence 确保字符串的每个单词首字母都大写,其余部分小写。 ...

  • 2017-12-21作文

    TITLE:College is not an ivory tower 【Title sentence】Colle...

网友评论

      本文标题:Title Case a Sentence | Free Cod

      本文链接:https://www.haomeiwen.com/subject/kqrctxtx.html