Created
July 27, 2020 07:39
-
-
Save keithn/9c60f875a8c5721830b449526e47c818 to your computer and use it in GitHub Desktop.
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
function myReplace(str, before, after) { | |
let isCapitalized = w => w[0].toUpperCase() === w[0] | |
let capitalize = w => w[0].toUpperCase() + w.slice(1) | |
let lower = w => w[0].toLowerCase() + w.slice(1) | |
let r = [] | |
for(let w of str.split(" ")){ | |
if(w.toLowerCase() === before.toLowerCase()) { | |
r.push(isCapitalized(w)?capitalize(after):lower(after)) | |
} else r.push(w) | |
} | |
return r.join(" ") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment