Hello. I’m trying to solve Search and Replace. Here is my code.
function myReplace(str, before, after) {
var res = str.split(" ");
for(var i=0; i < res.length; i++) {
if(res[i] === before) {
return after.replace(before); }
}
}
myReplace(“A quick brown fox jumped over the lazy dog”, “jumped”, “leaped”); //“leaped”`
I could print out ‘after’ word but I cannot combine them properly. I tried Array.prototype.join() but it print out with ‘before’ word.
function myReplace(str, before, after) {
var res = str.split(" ");
for(var i=0; i < res.length; i++) {
if(res[i] === before) {
after.replace(before);
return res.join();
}
}
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
// A ,quick, brown ,fox, jumped ,over, the, lazy ,dog
Could you give me some advice what is the problem? Thank you…