Title Case a Sentence-Code works but I cant't pass the test

My code for this challenge works fine. However, I can’t pass the challenge when I run tests. Here is my code:

function titleCase(str) {
str=str.toLowerCase();
var array=str.split(’ ');
for(var i=0;i<array.length;++i){
array[i]=array[i].replace(array[i].charAt(0),array[i].charAt(0).toUpperCase());

}
str="";
for(var j=0;j<array.length;++j){
str=str+array[j]+" ";
}
return str;
}

titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT”);

Shooting in the dark here, but does it want a semicolon after: for(var j=0;j<array.length;++j);

Your output string is not matching the tests because your second for loop adds a space after the last word.

Oh yes, I didn’t notice that. Thanks, I fixed it.

Ah no, in case of for loops you need curly brackets like {}. There is no need for semicolons.

1 Like