Intermediate Algorithm Scripting - Search and Replace

Tell us what’s happening:
Describe your issue in detail here.
I dont know why my code is not working .
Your code so far

function myReplace(str, before, after) {
  let pattern = new RegExp(before, "gi");
  return str.replace(pattern, after);

console.log(myReplace("He is Sleeping on the couch", "Sleeping", "sitting"));

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: Intermediate Algorithm Scripting - Search and Replace

Link to the challenge:

I think you need to reread the note:

Note: Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word Book with the word dog, it should be replaced as Dog

Where in your function are you preserving the case of the first character? Look at your console log. The result should be “He is Sitting on the couch” but your function is returning “He is sitting on the couch”. Do you have any ideas on how you can preserve the case so that your function returns “Sitting” instead of “sitting”?

1 Like

so How can I do it ?

Well, if I just tell you then you wouldn’t really be learning it on your own :slight_smile:

Can you explain in words how you might go about doing this? You don’t need to provide any code yet. Just give us a general idea on what you would do.

1 Like

Okay Ill give it a try …
Let suppose i have to change Word1 with word2 with case intact. If somehow I can store the case of Word1 in a new variable . then i can put word2 in that variable and change the case .
If i can make a function which take 2 words and stores the case of first word and change the case of the second word to the stores case.

Am i going somewhere ??

 return str.replace(pattern, after);

If you are not confident by RegExp, there is alternative like string.replaceAll()

if you want more controls over the logic when iterating an array, an basic for-loop is your choice. string.split("") might come in handy to split an string into array.

There are many ways to solve it, and i always suprised when re-look in to the solutions :smile:
Happy coding.

1 Like

After reading your explanation I’m still not convinced you completely understand what you need to do. Forget about storing things in variables. Pretend that I know nothing about programming. Give me the “Dummies” explanation of how you would do this.

1 Like

I can take the first word that needs to be replaced and i can put an if condition if the first letter of the first word is uppercase then the replacing word’s first letter should be uppercase too .
if first letter is lowercase the replacing word’s first letter should be lowercase too.
Am I going somewhere ?

function myReplace(str, before, after) {
  let newString = str.split(" ");
  for (let i = 0; i < newString.length; i+=1){

    if(newString[i] === before){
      if(newString.before[i][0] === newString.before[i][0].toUpperCase()){
      after = after[0].toUpperCase() + after.slice(1);
      newString[i] = after;
  return newString.join(" ");

myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

I dont know why this TypeError is occuring :
TypeError: Cannot read properties of undefined (reading ‘4’)

function myReplace(str, before, after) {
    after = after.charAt(0).toUpperCase() + after.slice(1);
  } else {
    after = after.charAt(0).toLowerCase() + after.slice(1);
  return str.replace(before, after);

console.log(myReplace("He is Sleeping on the couch", "Sleeping", "sitting"));

I think I did it. Thank You So Much for the help and your time.

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