Title Case a Sentence [Berlin]

Tell us what’s happening:
what seems to be the issue? this is the error message
titleCase(“I’m a little tea pot”) should return I’m A Little Tea Pot.

titleCase(“sHoRt AnD sToUt”) should return Short And Stout.

titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT”) should return Here Is My Handle Here Is My Spout.

Your code so far


function titleCase(str) {
  let words = str.split(' ');
  let updatedTitle = [];
  for (let x in words) {
    updatedTitle[x] = words[x].toLowerCase().replace(0, words[x].charAt(0).toUpperCase());
  }

  return updatedTitle.join(' ');
}

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

Your browser information:

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

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

Hey @Eghe,
Lets take first test case as example i.e titleCase(“I’m a little tea pot”)

The challenge wants your code to return I’m A Little Tea Pot.

Whereas your code returns "i'm a little tea pot".

What you are doing here is turning all the letters of every word in sentence to lowercase and challenge wants you to capitalize only the first letter of every word.

Try solving now.

The first argument to replace is the regexp (pattern), what are you expecting “0” to do?

I would, lowercase the full string, store the first letter of each word inside the for loop, run the replace on the words (words[x]), feed in the letter to replace for both the match and transform.