Hello everyone,
I try to solve “Search and Replace” algorithm question through using filter and map method. However, its unsuccessful attempt, and I cannot find where is my mistake. Everything seems perfect. Could u help me?
function myReplace(str, before, after) {
let splitted = str.split(" ");
return splitted
.filter(word => word == before)
.map(function(word) {
if(word[0] == word[0].toUpperCase()){
after = after[0].toUpperCase() + after.slice(1)
word = after
return word
} else {
word = after.toLowerCase()
return word
}
})
.join(" ")
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");