Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nirajkrz/3cb1fb4514bc4988f4ef to your computer and use it in GitHub Desktop.
Save nirajkrz/3cb1fb4514bc4988f4ef 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