Console ok, but cannot pass tests

Tell us what’s happening:

Working on the titleCase function, this is my solution, everything looks alright in the console, but the tests will not pass (except for the first one should return a string).

What should I do about it now?
What would be a better way to write this?

My code so far


function titleCase(str) {
  let arr = str.split(" ");
  let newTitle = "";
  for (let word of arr) {
    newTitle+= word[0].toUpperCase() + word.slice(1, word.length).toLowerCase() + " "
  }
  return newTitle
}

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

// should result: I'm A Little Tea Pot
// my result: I'm A Little Tea Pot

Challenge: Title Case a Sentence

Thanks!

Link to the challenge:

Your result contains an extra space in the end. Use trim method.

return newTitle.trim();
1 Like

I was wrong, didn’t really read it properly

1 Like