Why is my solution not working for the problem Search and Replace?

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

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/ Safari/537.36

Challenge: Search and Replace

Link to the challenge:

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(after, myBefore)

console.log(myReplace(“He is sleeping on the couch”, “sleeping”, “Sitting”));

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.