Develop the scrub
function that takes a string of text and an array of forbidden words. The function will replace any forbidden word in the text with a string of lowercase "x" characters, each "x" replacing one letter of the forbidden word. The modified text is then returned from the function.
function scrub(text, forbidden)
- text: A string representing the text to be scrubbed.
- forbidden: An array of strings, each a forbidden word to be replaced in the text.
- Replace each word in
text
that is included in theforbidden
array with a string of "x" characters of equal length to the forbidden word. - Ensure no punctuation is used in the text.
- If
text
is empty or there are no forbidden words, return the original text.
let result = scrub("out of the silent planet", ["of", "silent"]);
console.log(result);
Expected Output:
"out xx the xxxxxx planet"
let result = scrub("the ghost of the navigator", ["the"]);
console.log(result);
Expected Output:
"xxx ghost of xxx navigator"
let result = scrub("lost somewhere in time", []);
console.log(result);
Expected Output:
"lost somewhere in time"
let result = scrub("aces high", ["high", "aces", "hearts"]);
console.log(result);
Expected Output:
"xxxx xxxx"
let result = scrub("", ["high", "aces"]);
console.log(result);
Expected Output:
""
Utilize array methods like push
, split
, indexOf
, and join
to effectively transform the text.