Search & Replace

close up of laptop keyboard

From the FreeCodeCamp Intermediate Algorithms Challenges, here:

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).

FreeCodeCamp – Search & Replace

This is the seventh of 21 ‘Intermediate Algorithm Scripting’ lessons/challenges on FreeCodeCamp. That means I’m a third of the way through this section!

I’ve not really written too much about this one… it’s further practice with regular expressions and with both string and array methods. All of which is great practice.

Looking back 👀 at my code I can see that:

  • I split the given sentence into an array of words.
  • I checked each word in the array to see if it matched the given ‘before’ variable.
    • If it did match I assigned it to a new variable called ‘word’.
    • Then I checked to see if it had a capital letter at the beginning by using a regular expression test.
      • If it did I created a new string called ‘afterCap’ for the given ‘after’ word and assigned it the word but with a capital letter.
    • I then used the splice method to replace the ‘before’ word with either the given ‘after’ word or the ‘afterCap’ word based on if the ‘before’ word had a capital or not.
  • Finally I returned the array as a string by using the join method.

👍

My solution:

function myReplace(str, before, after) {
  let sentence = str.split(' ');
  for (let i = 0; i < sentence.length; i++) {
    if (sentence[i] == before) {
      let word = sentence[i];
      if (/[A-Z]/.test(word[0])) {
        let afterCap = after.charAt(0).toUpperCase() + after.slice(1);
        sentence.splice(i,1,afterCap);
      } else {
        sentence.splice(i,1,after);
      }
    }
  }
  return sentence.join(' ');
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

Published by Zachary

Learning to code with free code camp - zed.code.blog. Reading and writing at alwaysbooks.co.uk.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: