Title Case a Sentence - Not passing even if output is same

Tell us what’s happening:
Why is this code not passing the test cases even when the output is matching ??

Your code so far

function titleCase(str) {
  
  str1 = str.toLowerCase().split(" ");
  var str2 = "";
  for (var i =0; i<str1.length; i++){
    str2 += str1[i].charAt(0).toUpperCase() + str1[i].slice(1) + " ";
  }
  return str2;
  
}

titleCase("I'm a little tea pot");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/title-case-a-sentence

Think there is trailing space. You may need to add the space only if not last array element (str1).

Got it !!!

function titleCase(str) {
  str1 = str.toLowerCase().split(" ");
  var str2 = "";
  for (var i =0; i<str1.length; i++){
    if (i!=str1.length-1){
  str2 += str1[i].charAt(0).toUpperCase() + str1[i].slice(1) + " ";}
    else{str2 += str1[i].charAt(0).toUpperCase() + str1[i].slice(1); }
  }
  return str2;
  
}

titleCase("I'm a little tea pot");