Title Case a Sentence - 2018

I wonder why my code doesn’t work. specificaly the part the should capitalize the first letter of every word

Your code so far

SPLIT |str| TO AN ARRAY OF LETTERS
DEFINE LOCAL VARIABLE |toCAP| TO EVERY LETTER THAT COMES AFTER A SPACE |" "|
IF A LETTER COMES AFTER A SPACE
  CAPITALIZE  |LETTER|
  PUSH |LETTER| INSTEAD
ELSE
  |LETTER| IS DEFINED AS THE LETTER
  |LETTER| IS LOWERCASED
  |LETTER| IS PUSHED TO THE LETTER INDEX
 RETURN THE ARRAY WITHOUT SPACERS
*/
function titleCase(str) {
  str.toLocaleLowerCase();
  var letterArray = str.split("");
  var letter = "";
  for (var i = 0; i < letterArray.length; i++) {
    if (i == 0) {
        letter = letterArray[0];
        letter.toUpperCase();
        letterArray[0] = letter;
    } else if (letterArray[i - 1] == " ") {
        letter = letterArray[i];
        letter.toUpperCase();
        letterArray[i] = letter;
    }
  } 
  return letterArray.join("");
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/title-case-a-sentence

String functions (like toUpperCase) never change the string they’re called with. They produce new strings, which you need to assign somewhere.

1 Like

According to the docs:

The toUpperCase() method returns the value of the string converted to upper case. toUpperCase() does not affect the value of the string itself.