CodeNewbie Community

Aaron McCollum
Aaron McCollum

Posted on • Updated on

Intermediate Algorithm Scripting: Search and Replace

From time to time, I hope to post my answers to various coding challenges, showing the thought process and raw code I am writing. Fair warning - it might not be pretty. However I'd appreciate tips to make it cleaner!

I'm almost through freeCodeCamp's JavaScript course, and currently I am slowly making my way through the intermediate algorithm section. These challenges are no joke! Below is a walk-through on how I solved one of the challenges "Search and Replace."

Prompt: Perform a search and replace on the sentence using the arguments provided and return the new sentence. First argument is the sentence to perform the search and replace on. Second argument is the word that you will be replacing (before). Third argument is what you will be replacing the second argument with (after). 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"

This starts out easy enough. Get a string, find the before word, and replace it with the after word. A simple "for" loop with the Array.splice() method would take care of that in a jiffy!

However, the last part of the prompt is what makes this problem trickier. If the first letter in the before word is capitalized, then we need to make sure the first letter in the after word is also capitalized, and vice-versa. The tests do include checks for this (ex. before = "Running" and after = "walking"). Our code will need to change "walking" to "Walking" in order to pass the test.

Here is my code:
Alt Text

Here is how I broke the problem down:

  1. Split the string into an array of words called strArray.
  2. Loop through strArray with a "for" loop.
  3. Use an "if" statement to find the word in the string which matches the before word. Once the loop finds it, it triggers the rest of the code.
  4. IF the first letter of the matched word in strArray equals the capitalized version of that word, then we create a variable to contain a capitalized version of after. (The splice() method will remove the extra letter caused by the .concat() in the line above).
  5. ELSE IF the first letter of the matched word in strArray equals the lowercase version of that word, then we create a variable to contain a lowercase version of after.
  6. ELSE (if both start with lowercase or uppercase letters), we just reassign that element to equal after.
  7. Return strArray with the join() method to turn it back into a string!

In order to achieve this, I created helper functions on the side. I've started doing this a lot to help make the solution in my main function easier to read, and easier for me to write. If you are like me and learning to go through algorithm problems, helper functions on the side are fun to write, since they give you extra practice writing functions to use in a larger program (and they are generally simpler than the actual problem itself!).

Anyways that's how I tackled it! I googled a few things on my own, but I try to avoid looking at the fCC solutions until I solve it on my own. On this one, I looked at the solutions after I had solved it and found a really easy way to write it in about 8 lines total (instead of 30+ lines with multiple helper functions). Whoops!

But in general, this was a fun one. How would you go about solving this?

Discussion (0)