Skip to content

Instantly share code, notes, and snippets.

@newbenhd
Created May 24, 2019 19:28
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 newbenhd/65a0609a5afb4fcae6b4be7a7d053222 to your computer and use it in GitHub Desktop.
Save newbenhd/65a0609a5afb4fcae6b4be7a7d053222 to your computer and use it in GitHub Desktop.
Censor function
// 1. Declare a function named censor with no parameter.
// 1-1. Declare a backpack variable named censorObject, and assign it an empty object.
// 1-2. Declare a function named getCensor with two parameters: a string named str1, a string named str2.
// 1-2-1. use if statement to check if str2 is not undefined. If so, set censorObject's key to str1, and its value to str2.
// 1-2-2. Else, iterate through censorObject using for...in looop
// 1-2-2-1. Check if any of keys' strings found on str1's string. If so, replace the string with the value associated with key using regex and String.replace method.
// 1-2-2-2. Return str1.
// 1-3. Return getCensor.
function censor() {
let censorObject = {};
console.log("how does this code works?!");
function getCensor(str1, str2) {
console.log(censorObject);
if (typeof str2 === "string") {
censorObject[str1] = str2;
} else {
for (let key in censorObject) {
const reg = new RegExp(`${key}/g`);
console.log(reg);
str1 = str1.replace(reg, censorObject[key]);
}
return str1;
}
}
return getCensor;
}
// Uncomment these to check your work!
const changeScene = censor();
changeScene("dogs", "cats");
changeScene("quick", "slow");
console.log(changeScene("The quick, brown fox jumps over the lazy dogs.")); // should log: 'The slow, brown fox jumps over the lazy cats.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment