Search and Replace: toUpperCase toLowerCase

Tell us what’s happening:
I believe this is working mostly, but I am stumped trying to figure out how the toUpperCase should work. The toLowerCase works, so I feel close. I am trying to not look at hints, but rather dig around for insight. I might try a completely different approach soon. But I feel so close that I might be making a typo?

  **Your code so far**

function myReplace(str, before, after) {

var strArr = str.split(" ");

if (before.charAt(0) === /[A-Z]/) {
  strArr.splice(strArr.indexOf(before), 1, after.charAt(0).toUpperCase() + after.slice(1));
} else if(before.charAt(0) != /[A-Z]/) {
  strArr.splice(strArr.indexOf(before), 1, after.charAt(0).toLowerCase() + after.slice(1));
}
console.log(strArr.join(" "));
return strArr.join(" ");
}
console.log(myReplace("I think we should look up there", "up", "Down"))
console.log(myReplace("He is Sleeping on the couch", "Sleeping", "sitting"))
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36

Challenge: Search and Replace

Link to the challenge:

you can’t use regex like this, you need to use a method that uses regex, you can’t compare it directly to a string

1 Like

Thank you… in the time it took to get the reply, I updated those to if (before[0].match(/[A-Z]/)) and if (before[0].match(/[a-z]/))

Now it’s working. The Hawthorne Effect is real.

Thanks again.

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