Tell us what’s happening:
Describe your issue in detail here.
**Your code so far**
function myReplace(str, before, after) {
let myArr = before === after ? 0 : after.charAt(0).toUpperCase() + after.slice(1)
return str
.replace(before,myArr)
}
console.log(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/102.0.0.0 Safari/537.36
the myArr was just something I came up with on the spot. I should change that to myStr. I’m trying to figure out a way that instead of having before ===after I can check if the first letter is lowercase in before and after. There for just returning after before === after ? after : after.charAt(0).toUpperCase() + after.slice(1). I guess my only issue is to find a way to do that.
I ended up figuring out using my method. What do you think of it?
function myReplace(str, before, after) {
let myAfter = before[0].toUpperCase() === before[0] ? after.charAt(0).toUpperCase() + after.slice(1) : after
let myBefore = before[0].toLowerCase() === before[0] ? after.charAt(0).toLowerCase() + after.slice(1): before
return str
.replace(before,myAfter)
.replace(after, myBefore)
}
console.log(myReplace(“He is sleeping on the couch”, “sleeping”, “Sitting”));