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

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

#1

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(' ');

console.log(words);
console.log(words.length);
for (let i = 0; i < words.length; i++) {
lower = words[i].toLowerCase();
console.log(lower);
upper = lower.charAt(0).toUpperCase() + lower.substr(1);
console.log(upper);
  newt = newt + " " + upper;
  console.log(newt);
}
console.log(newt);
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:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence


#2

It looks like you’re probably adding extra spaces.


#3

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


#4

Glad to help. Happy coding.