1.4 KiB
1.4 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
ab6137d4e35944e21037b769 | 句中單詞首字母大寫 | 5 | 16088 | title-case-a-sentence |
--description--
請將傳入的字符串中,每個單詞的第一個字母變成大寫並返回。 注意除首字母外,其餘的字符都應是小寫的。
在這個挑戰中,我們還需要將諸如 the
和 of
之類的連接詞大寫。
--hints--
titleCase("I'm a little tea pot")
應返回一個字符串。
assert(typeof titleCase("I'm a little tea pot") === 'string');
titleCase("I'm a little tea pot")
應返回 I'm A Little Tea Pot
。
assert(titleCase("I'm a little tea pot") === "I'm A Little Tea Pot");
titleCase("sHoRt AnD sToUt")
應返回 Short And Stout
。
assert(titleCase('sHoRt AnD sToUt') === 'Short And Stout');
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT")
應返回 Here Is My Handle Here Is My Spout
。
assert(
titleCase('HERE IS MY HANDLE HERE IS MY SPOUT') ===
'Here Is My Handle Here Is My Spout'
);
--seed--
--seed-contents--
function titleCase(str) {
return str;
}
titleCase("I'm a little tea pot");
--solutions--
function titleCase(str) {
return str.split(' ').map(word => word.charAt(0).toUpperCase() + word.substring(1).toLowerCase()).join(' ');
}
titleCase("I'm a little tea pot");