Title Case Sentence

Tell us what’s happening:

This passes all strings I put in it but it won’t show as passed.

Your code so far


function titleCase(str) {
  let splitStr = str.split(' ');
  let emptyStr = '';
  for(var i = 0; i < splitStr.length; i++) {
  var upperStr = splitStr[i][0].toUpperCase() + splitStr[i].slice(1).toLowerCase(); 
    emptyStr += upperStr + ' ';
  }

  return emptyStr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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

Every time you add a word to emptyStr, you also add a space too the end. That’s fine, but to fix it see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim

:wink:

That fixed it! Thank you so much!

1 Like