Skip to content

Instantly share code, notes, and snippets.

@johnnyji
Last active November 9, 2019 18:20
Show Gist options
  • Save johnnyji/25ae08d09d8aa3c1f864e1fb3b83cb88 to your computer and use it in GitHub Desktop.
Save johnnyji/25ae08d09d8aa3c1f864e1fb3b83cb88 to your computer and use it in GitHub Desktop.
String Replacer
// Write a function that will take a string with word placeholders (ie. "Hello __NAME__"), and an
// options object (ie. {name: "James"})and return that string with placeholder words replaced by values
// in the options object (ie. "Hello James")
// For this example, we can assume that each __PLACEHOLDER__ in the string will have a corresponding key in the options object.
// So the word `__PLACEHOLDER__` in the string will have a key value of `placeholder` in the options object. Let's look
// at a quick example:
// const string = "James loves to play __SPORT__ on __DAYOFWEEK__"
// const opts = {sport: "soccer", dayOfWeek: "Sunday"};
// Once passed through our stringReplacer function, the output should be:
// "James loves to play soccer on Sunday".
// Some things to think about:
//
// 1) The format of the placeholder is slightly different than the key in the options object, so how can we match them together?
// 2) We don't know what the string passed in will be, so there could be any amount of different placeholders
// in a the string given to us, however we do know that each different placeholder in the string will for sure have a
// corresponding key in the options object
//
// I look forward to seeing your thought process in solving this problem!
//
// - Johnny
const stringReplacer = (string, opts) => {
// Code here
};
const string = '__NAME__ came to our __EVENT__ and __ACTION__';
const opts = {
name: 'Jared',
event: 'movie',
action: 'loved it'
};
console.log(stringReplacer(string, opts));
// The correct output here should be: "Jared came to our movie and loved it"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment