Hi guys, could someone please explain how this code is working?
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");
Specifically, what exactly is happening when something is passed to replaceAt? What exactly is passed to it, and what does replaceAt do with it? Specifically this line: this.substr(0, index) + character + this.substr(index+character.length)
. I don’t get heads or tails of what that line is doing, what it’s adding, and what the end product is that it’s returning.