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?