Last active
May 24, 2019 19:10
-
-
Save newbenhd/ab9d349e5ff7710d17fcefdf5d687807 to your computer and use it in GitHub Desktop.
Censor function
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
/* | |
Challenge: | |
Create a function censor that accepts no arguments. censor will return a function that will accept either two strings, or one string. When two strings are given, the returned function will hold onto the two strings as a pair, for future use. When one string is given, the returned function will return the same string, except all instances of a first string (of a saved pair) will be replaced with the second string (of a saved pair). | |
*/ | |
// 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("Hello"); | |
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