I'm struggling to figure out how to replace more than one character at a specific index in a string.
I attempted using a FOR loop, but it didn't yield the desired results.
String.prototype.replaceAt=function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}
var str = "hello world";
var indices = [1, 4, 9];
for(i = 0; i < indices.length; i++) {
str.replaceAt(indices[i], "?");
}
After the loop, the str
should be "h?ll? wor?d"
, but it remains as "hello world"