Continuing the discussion from freeCodeCamp Algorithm Challenge Guide: Search and Replace:
function myReplace(str, before, after) {
var aftStr = after.split("");
var bfrStr = before.split("");
for(i=0;i<bfrStr.length;i++){
if(bfrStr[i] === bfrStr[i].toUpperCase())
aftStr[i] = aftStr[i].toUpperCase();
}
var afterString = aftStr.join("");
var newString = str.replace(before,afterString);
return newString;
}
myReplace("His name is Tom", "Tom", "john");