Title Case a Sentence - console.log logs as complete but not passing tests

Tell us what’s happening:
The results when I do console.log seem to be what the challenge is asking for, yet it is not passing the tests. Any advice or insight?

Your code so far

function titleCase(str) {
let lower = '';
let upper = '';
let words = '';
let newt = '';
words = str.split(' ');

for (let i = 0; i < words.length; i++) {
lower = words[i].toLowerCase();
upper = lower.charAt(0).toUpperCase() + lower.substr(1);
  newt = newt + " " + upper;
return newt;

titleCase("sHoRt AnD sToUt");

Your browser information:

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

Link to the challenge:

It looks like you’re probably adding extra spaces.

1 Like

Yeah you’re right; I did a Reg Ex to remove the spaces at the beginning and end and it worked. Thanks!

Glad to help. Happy coding.