It just seems like the advanced solution does a lot of extra work and takes up a lot more extra power to use. Would anyone like to explain? If you were going to use this function in a program, which of the provided solutions would be best to use?
Also, here’s my solution if you want to comment on it:
function myReplace(str, before, after) {
if(before[0].match(/[A-Z]/)) return str.replace(before, after[0].toUpperCase() + after.slice(1));
return str.replace(before, after);
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");