Forked from anonymous/bonfire-search-and-replace.js
Created
December 3, 2015 02:33
-
-
Save nirajkrz/3cb1fb4514bc4988f4ef to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Search and Replace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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