Search and replace algorithm help

//somone please help me i get all the marks for this right, only one wrong, being third from the top. Don't know what i'm doing wrong here


function myReplace(str, before, after) {

   if (before.charAt(0) === before.charAt(0).toUpperCase()) {
 after = after.charAt(0).toUpperCase() + after.slice(1);

  }

   return str.replace(before, after);

}

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

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Can you please describe what isn’t working corrrectly? Is there a part of the code that you’re confused about?

okay thanks, nothing really i’m not understanding, i gave someone else my code to try and they said it had worked for them, here’s the link search and replace

I’ve run the tests and it fails for me as well.

Reviewing the code, i think there is a misunderstanding of the request.

Key point: In the requested word replacements, you are to preserve the capitalization of the original string.

For the test that is failing for example; in the original string, the word “up” is lowercase, so you are to replace it with the word “Down”. However preserving the original capitalization of “up” (all lowercase).

You can see from the screenshot that when the string is replaced, the “Down” word is still capitalized.

You’ll need to revisit your logic to meet that requirement. I don’t wanna spoil it for you :slight_smile:

1 Like

ohh wow that helps a lot, completely missed this, thanks man

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