Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2015 03:34
Show Gist options
  • Save anonymous/d3788626101e338d1f47 to your computer and use it in GitHub Desktop.
Save anonymous/d3788626101e338d1f47 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/tylerl-uxai 's solution for Bonfire: Search and Replace
// Bonfire: Search and Replace
// Author: @tylerl-uxai
// 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) {
var i = str.indexOf(before);
if (str[i] == str[i].toUpperCase()){
after = after.charAt(0).toUpperCase() + after.slice(1);
}
return str.replace(before, after);
}
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