Title Case a Sentence - what's wrong?

The task:
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
For the purpose of this exercise, you should also capitalize connecting words like “the” and “of”.

My solution:
function titleCase(str) {
var arr=str.split(" “);
var newstr=”";
for (i=0; i<arr.length; i++){
newstr+=arr[i].charAt(0).toUpperCase();
for(j=1; j<arr[i].length;j++){
newstr+=arr[i].charAt(j).toLowerCase();
}
newstr+=" ";
}
return newstr;
}
titleCase(“I’m a little tea pot”);
titleCase(“sHoRt AnD sToUt”);
titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT”);

Returned string is right, but test isn’t passed. Where is the problem?

Let me guess without looking at the code. Are you adding an unnecessary space after sentence? :smirk:

1 Like

There is extra space at the end of your result which causes your tests to fail.

Simply do return newstr.trim(); and you should be good.

1 Like