my code is supposed to take a word (before) and replace it with another word(after) inside of a string(str). my code replaces the words easily however i am having trouble getting the replaced word to have the same case as the original.
function myReplace(str, before, after) {
if(before[0].toUpperCase() === before[0]){
after[0].toUpperCase()
} else {
after[0].toLowerCase()
}
str = str.replace(before, after);
console.log(str)
return str
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.72
Challenge: Search and Replace
Link to the challenge: