这个编译答案应该是对的,但是通过不了,请问这是为什么呢?
Challenge Title Case a Sentence has an issue.
User Agent is: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
function titleCase(str) {
var temp =str.toLowerCase();
var arr =[];
var arr1=[];
str="";
arr = temp.split(" ");
for(var i=0;i<arr.length;i++){
arr1=arr[i].split("");
arr1[0]=arr1[0].toUpperCase();
str+=arr1.join("")+" ";
}
return str;
}
titleCase("I'm a little tea pot");
你的结果末尾多了一个空格
这个不是 bug。你这种写法,可以最后 return str.trim();
function titleCase(str) { // 请把你的代码写在这里 var Str = str; var newArray = Str.toLowerCase().split(" "); var newStr=""; for(var i=0;i<newArray.length;i++){
newStr += newArray[i].substring(0,1).toUpperCase()+newArray[i].substring(1,newArray[i].length)+" ";
}
return newStr.trim(); }
titleCase("I'm a little tea pot");
这个是我的写法,想知道还有没有别的写法了
@changeyan 方法很多。http://singsing.io/blog/fcc/basic-title-case-a-sentence/
function titleCase(str) {
// 请把你的代码写在这里
var arr = str.toLowerCase().split(" ");
var stringArr = [];
arr.forEach(function (item) {
item = item.replace(item[0], item[0].toUpperCase());
stringArr.push(item);
});
return stringArr.join(" ");
}
function titleCase(str) { // 请把你的代码写在这里 var arr=str.toLowerCase().split(' '); var newArr=[]; arr.map(function(val){
newArr.push(val.replace(val[0],val[0].toUpperCase()));
}); return newArr.join(' '); }
titleCase("I'm a little tea pot");