Title Case a Sentence wont pass seems right

Tell us what’s happening:
Is there something wrong with my code here. It seems to have the desired result but in a slightly different way. It wont pass it and I’m just wondering why. Is putting it straight into a string less efficient than putting it into an array and then joining with a space or is there something else I am missing out here? Thank you :slight_smile:

Your code so far

function titleCase(str) {
  var myArr;
  var test = '';
  myArr = str.split(' ');
  for(var i=0; i<myArr.length; i++) {
   myArr[i] = myArr[i].toLowerCase();
   test += myArr[i].replace(myArr[i].charAt(0), myArr[i].charAt(0).toUpperCase()) + ' '; 
  return test;


Your browser information:

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

Link to the challenge:

The above line always adds a space character to the end, so the final value of test will have an extra space character at the end. That is why it is not passing the tests.

That is wonderful thank you ha :smiley: