I used String.prototype.replaceAt to solve the Intermediate Algorithm challenge Search and Replace b/c it was the solution for Beginner Algorithm Title Case a Sentence. Why wasn’t this mentioned in the solution for Intermediate Level Search and Replace?
String.prototype.replaceAt = function(index,character){
return this.substr(0,index) + character + this.substr(index + character.length);
};
function myReplace(str,before,after){
if (before[0] === before[0].toUpperCase()){
altAfter = after.replaceAt(0, after.charAt(0).toUpperCase());
return str.replace(before, altAfter);
}
return str.replace(before,after);
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
The above code works. I’m just wondering why the solution from a previous challenge wouldn’t be mentioned as possible solution for this challenge. It builds upon what we know.