Title Case a Sentence 4

Tell us what’s happening:

Hi, campers.

Can someone explain to me why this code is not working?

Your code so far


function titleCase(str) {

let newStr = str.toLowerCase().split(" ");

for(let i = 0; i < newStr.length; i++) {
  let firstLetter = newStr[i].charAt(0);
  newStr[i].replace(firstLetter, function (letter) {
    letter.toUpperCase();
  });
}
return newStr.join(" ");
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

Pretty sure it because of how replace works.

The replace() method returns a new string

The original string is left unchanged.

If you make a new array and push the result of the replace into it and return that you should pass.

Thanks lasjorg. Now it’s clear to me