function titleCase(str) {
var temp = "";
var arr;
var stri = str.toLowerCase();
var words = stri.split(" ");
for (var i = 0; i < words.length; i++)
arr = arr + words[i].replace(words[i].charAt(0), function(temp) {
return temp.toUpperCase();
});
return arr;
}
titleCase("I'm a little tea pot");
答案 0 :(得分:0)
您可以使用arr.join(" ");
。
答案 1 :(得分:0)
您可以拆分字符串,将第一个字符作为大写字母并添加字符串的其余部分。最后,将数组连接到带空格的字符串。
function titleCase(string) {
return string
.split(' ')
.map(s => s[0].toUpperCase() + s.slice(1))
.join(' ');
}
console.log(titleCase("I'm a little tea pot"));
&#13;