Bonfire Search and Replace - GJSmith3rd/FreeCodeCamp-BootCamp GitHub Wiki

Contact me

Gilbert Joseph Smith III

@gjsmith3rd

Github | FreeCodeCamp | CodePen | LinkedIn | Blog/Site | E-Mail

Details

  • Difficulty: 2/5

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

NOTE: Preserve the case of 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'

Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.

Useful Links

Problem Script:

function replace(str, before, after) {
 return str;
}

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

Problem Explanation:

  • You will create a program that takes a sentence, then search for a word in it and replaces it for a new one while preserving the uppercase if there is one.

Hint: 1

  • Find the index where before is in the string.

Hint: 2

  • Check first letter case.

Hint: 3

  • Strings are immutable, you will need to save the edits on another variable, even if you must reuse the same one just to make it look like the changes where done using just that one variable.

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

Code Solution:

function replace(str, before, after) {
  // Find index where before is on string
  var index = str.indexOf(before);
  // Check to see if the first letter is uppercase or not
  if (str[index] === str[index].toUpperCase()) {
    // Change the after word to be capitalized before we use it.
    after = after.charAt(0).toUpperCase() + after.slice(1);
  }
  // Now replace the original str with the edited one.
  str = str.replace(before, after);

  return str;
}

Code Explanation:

  • Read comments on program.