In Title case problem output is correct in console but test case is not passing.why?

Tell us what’s happening:

Your code so far

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

  for(var i = 0; i < str.length; i++)
    {
      str[i] = str[i].split("");
      str[i][0] = str[i][0].toUpperCase();
      str[i][str[i].length] = " ";
      str[i] = str[i].join("");
    }
  
  str = str.join("");
  
    console.log(str);
  
  return str;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/60.0.3112.113 Chrome/60.0.3112.113 Safari/537.36.

Link to the challenge:

titleCase(“I’m a little tea pot”) should return “I’m A Little Tea Pot”.

Your code returns "I’m A Little Tea Pot ". Note the extra space at the end of the sentence.

Thanks. Any suggestion how to remove that?

The easiest way to fix this, is using trim().

Thank u.The code is now working properly