Title Case a Sentence: function works but does not pass [SOLVED]

Hi, I wrote this function for the Title Case a Sentence challenge, the code seems to work fine but it doesn’t pass the challenge:

function titleCase(str) {
  var array = str.toLowerCase().split(" ");
  str = "";

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

  return str;
}

titleCase("sHoRt AnD sToUt");

What should I do?
Sorry for my bad english!

It is nearly correct. You always add a space " " at the end of the sentence, so you get "I'm A Little Tea Pot ", where it should be: "I'm A Little Tea Pot".

You can use .trim() to remove any spaces at the end (and also on the front).

1 Like

Thank you so much, it worked!