Title Case a Sentence - can't pass it [SOLVED]

Title Case a Sentence - can't pass it [SOLVED]
0

#1

Hello,

I’ve just finished my new challenge ‘Title Case a Sentence’. My algorithm seems be OK. After run test all first letters of each word are capitalized, but for some reason I can’t pass it :-/

Here is a code:

function titleCase(str) {
  
  var oldStr = str.toLowerCase().split(" ");
  
  
  var newStr = "";
  for(var i = 0; oldStr.length > i; i++){
    
     newStr += oldStr[i].replace(oldStr[i].charAt(0), oldStr[i].charAt(0).toUpperCase()) + " ";
   }
 
  
  return newStr;
}

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

#2

Check if you are adding unnecessary spaces at the beginning or end of the string.
It’s because “some string” is not equal to "some string ".:wink:


#3

Now my code pass, but the algorithm looks a bit messy :-/

function titleCase(str) {
  
  var oldStr = str.toLowerCase().split(" ");
  
  
  var newStr = "";
  for(var i = 0; oldStr.length > i; i++){
    
     newStr += oldStr[i].replace(oldStr[i].charAt(0), oldStr[i].charAt(0).toUpperCase()) + " ";
    
   }
 
   return newStr.replace(/\s*$/, "");
}

titleCase("sHoRt AnD sToUt");

I had to add regex to return function as I could not figure out another alternative. Anyway thx, for pointing me out empty string:-)