Proper Case a Sentences

Tell us what’s happening:

This code works. However the tests fail. The “algorithm” is supposed to title case a string of words.
All words should be proper case, e.b. hElLo should be Hello or HELLO TO YOU should be Hello To You.

I tested my code in VSCode and on Playcode, both work fine. FCC Tests fail. Any ideas?

Your code so far


function titleCase(str) {
   var val =  str.split(' ');
   var retVal = "";
   for(var i=0; i<val.length; i++){
     retVal += val[i].charAt(0).toUpperCase() + val[i].slice(1).toLowerCase();
   }
   return retVal;
}


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

You are missing some spaces.

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