Title Case a Sentence - getting expected output but not passing?

Tell us what’s happening:

As per title, my rough solution (compared to the example solution provided), produces the expected output but it’s not passing.

Any ideas why? Is the program expecting a particular method to solving this problem?

Thank you.

First time posting so apologies if this is in the incorrect place.

Your code so far


function titleCase(str) {
  let t = "";
  let arr = str.split(' ');
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      if (j === 0) {
        t += arr[i].charAt(j).toUpperCase();
      } else {
        t += arr[i].charAt(j).toLowerCase();
      }
    }
     t += " ";
  }
  return t;
}

console.log(titleCase("I'm a little tea pot"));
console.log(titleCase("sHoRt AnD sToUt"));
console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

You may have an extra space at the end of the string you are returning.

You’re right. I can’t believe I missed that!

Thank you.