Title Case a Sentence why this doesn't work

For this one I can see that the uppercasing didn’t work but I want to know why so I can understand where I went wrong. I will map through it as per the solution but I wanna know why this didn’t work with the for loop thank you

function titleCase(str) {
  let newStr = str.toLowerCase().split(" ");
  for (let i = 0; i < newStr.length; i++) {
    newStr[i].replace(newStr[i].charAt(0), newStr[i].charAt(0).toUpperCase())

    return console.log(newStr.join(" "));

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.

Hi can you see where I went wrong in my code that is what I wanted to know thank you

You should read the documentation I linked. The replace method does not change the original string, it returns a new string.

1 Like

Adding to the above, a string is a primitive that’s immutable.

1 Like

Oh Okay got it thank you so much really helped

Got it thank you so much

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.