Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2015 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/100348ec0b2d2a7003b5 to your computer and use it in GitHub Desktop.
Save anonymous/100348ec0b2d2a7003b5 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Search and Replace
// Bonfire: Search and Replace
// Author: @nirajkrz
// Challenge: http://www.freecodecamp.com/challenges/bonfire-search-and-replace
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function myReplace(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;
}
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment