Title Case a Sentence - Free Code Camp

Hi All,
The following code shows the correct result in the console output, but unable to submit the challenge while running the test. Could you please help me?
Thanks

function titleCase(str) {
  str = str.split(' ');
  console.log(str);
  let newString = '';
  for(let i=0; i<str.length; i++){
    //console.log(array[i]);
    newString = newString + str[i].charAt(0).toUpperCase(); 
    newString = newString + str[i].slice(1).toLowerCase() + ' ';
  }
  console.log('new string:', newString);
  return newString;
}

titleCase("sHoRt AnD sToUt");

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

you have an extra space at the end of the string

Hi ieahleen,
Thanks for your kind response. If the space is removed at the end, the output will look like this: ShortAndStout
Thanks

the only unnecessary space is at the end of the string
you have "Short And Stout "

I quoted where it comes from

Thank you very much, ieahleen. I fixed it with .trim() method. You are so kind and generous. Thanks again.