Tell us what’s happening:
Hi, I am somehow confused with the use of “this” in this excersice, the function works good I borrow the sample from the get a hint, it is supposed to be a basic solution, but until now I find the intermediate solution way easier than this, also the explanation on how the function works is unclear, can anyone help me so i can get a more understanding of what is happening in here ? I get the most part but things like “this” “substr” etc, are so confusing, thanks.
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
Your code so far
String.prototype.replaceAt = function (index, character) {
let result = this.substr(0, index) + character + this.substr(index + character.length);
return result;
};
function titleCase(str) {
let newTitle = str.split(' ');
let updatedTitle = [];
for (let st in newTitle) {
updatedTitle[st] = newTitle[st].toLowerCase().replaceAt(0, newTitle[st].charAt(0).toUpperCase());
}
return updatedTitle.join(' ');
}
console.log(titleCase("I'm a little tea pot"));
// Output : I'm A Little Tea Pot
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
.
Link to the challenge: