Challenge "Title Case a Sentence"

Please help me. I don’t understand what I’m doing wrong if all conditions execute. But It doesn’t get me ahead.

`function titleCase(str) {
var text = str.toLowerCase();
var arr = text.split(" ");
// return arr.length;
var txt = " ";
var i;
for(i=0; i < arr.length; i++) {
txt += arr[i][0].toUpperCase() + arr[i].slice(1) + " " ;
}
return txt;
}

titleCase(“sHoRt AnD sToUt”);`

It’s really hard to see, but you’re returning the right string with an extra space at the end. "Short And Stout " !== “Short And Stout”. Try to figure out a way to return that same string, but without the space at the end.

Maybe here, u have also extra space at beginning:
var txt = " ";
!==
var txt = "";

And also extra space at the end like pointed @PortableStick

Good catch! I didn’t think about the space at the beginning.

Thank’s for hint but I still don’t understand how it could be fixed.

Think about it for a little while. You have an extra space at the beginning and at the end. It’s just another challenge to solve.

1 Like