function titleCase(str) {
var arr=str.split(" ");
var newstr="",word="";
for(var i=0;i<arr.length;i++){
arr[i]=arr[i].toLowerCase();
var temparr=arr[i].split("");
temparr[0]=temparr[0].toUpperCase();
word=temparr.join("");
newstr+=" "+word;
}
console.log(newstr);
return newstr;
}
titleCase("I'm a little tea pot");
I am gating the right output in console but the challenge is not completing and showing an error! can someone help me where i am getting wrong. Thanks in advance.
The length of your string after doing a titleCase() is 1 more than the original string length.
You have a leading space in front of your output string. That’s causing the test to fail.
you don’t need the variable newstr and word.
also, instead of doing a toLowerCase() on each character of word, just apply it to the whole sentence in the beginning.
function titleCase(str) {
var arr = str.toLowerCase().split(' ');
var temparr = [];
for(var i=0; i<arr.length; i++){
temparr = arr[i].split('');
temparr[0]=temparr[0].toUpperCase();
arr[i] = temparr.join('');
}
return arr.join(' ');
}
titleCase("I'm a little tea pot");