Title Case a Sentence wont pass seems right

Title Case a Sentence wont pass seems right
0

#1

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;
}

titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");

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:


#2

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.


#3

That is wonderful thank you ha :smiley: