I have a serious problem understanding the code.
function replace(str, before, after) {
var rtt = before;
var rt;
if(before[0] === before[0].toUpperCase()) {
var aa = after.split("");
var u = after[0].toUpperCase();
aa.splice(0,1,u);
rt = aa.join("");
}
else {
rt = after;
}
var r = str.split(" ");
var i = r.indexOf(rtt);
r.splice(i,1, rt);
var join = r.join(" ");
return join;
}
replace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
I don't understand why I can't just do r.join(" ") after r.splice(i,1,rt) and then return r? Why I must declare new variable join? otherwise return will not return correct statement. The code is correct though.
r = r.join(" ");instead other than a new variable. Butr.join(" ")will return a value and not set it, so that's why.r.splice(i,1, rt); return r.join(" ");.