Title Case a Sentence - Error

I am a newbie.

Please help me.
Tell me what’s happening:

Your code so far


String.prototype.replaceAt = function(index, character) {
    return this.substr(0, index) + character + this.substr(index+character.length);
};

function titleCase(str) {
  var newTitle = str.split('');
  var updatedTitle = [];
  for (var st in newTitle) {
    updatedTitle[st] = newTitle[st].toLowerCase().replaceAt(0, newTitle[st].charAt(0).toUpperCase());

  }
  return updatedTitle.join(' ');
  }

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



/*     var newTitle = str.split(' ');
    var updatedTitle = [];
    for (var st in newTitle) {
        updatedTitle[st] = newTitle[st].toLowerCase().replaceAt(0, newTitle[st].charAt(0).toUpperCase());
    }
    return updatedTitle.join(' ');
} */

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/

There is no built-in replaceAt function in JavaScript. You would need to create such a function.

They have created such a method, it is above the titleCase function declaration

Oops, I somehow missed that.

@ZeroOne Since your replaceAt method is only receiving one character, it is only going to return one character (which is just the same character capitalized). That is why you are just returning a string with all of the characters capitalized.

This line here is splitting the input string into individual characters. This means your loop is replacing every single letter instead of the first letter of each word. You will want to split on the space character str.split(' ') instead of empty string.

Another note while we’re here, it is generally not a good idea to extend the prototype of a built-in class like String with your own methods, but for a challenge like this it’s not a big deal.

re

it is generally not a good idea to extend the prototype of a built-in class like String with your own methods

This would seem a good intro to the Cardinal Rule of SW Development:

Maximize cohesion; minimize coupling

Many design patterns are simply examples of how to avoid naively or carelessly violating this rule.

The reason why one doesn’t extend the prototype of a built-in class like String is because it introduces a global coupling to every usage of the string type throughout the entire scope of the application.

Thank you very much. Have a good day.