How to make a word filter? (gamelab)

So, I have been trying to make a word filter which would work like this:

var prompt = prompt("put a word"); 
var words = ["wordOne", "wordTwo", "wordThree"]
//now below is the part i cant figure out
if (prompt == words) {
 prompt = "i said a blacklisted word" 

If you want it to completely stop users from chatting with inappropriate language, you would have to loop through the entire array.

var words = ["wordOne", "wordTwo", "wordThree"];
for (var i in words) { 
  if (result.includes(words[i]) { // you can't use prompt as a variable
    result = "i said a blacklisted word";

With only a few blacklisted words, this would be fine. However, if you have a lot of blacklisted words, it will take much longer to filter, resulting in slowdowns and dropped frames.
This may also have additional issues, such as the synonym for “behind” (iykyk) being recognized in words such as “glasses”.
What I would do, instead, is use RegEx to determine swear words, and replace them with asterisks:

var i = prompt("put a word");
i = i.replace(/wordone/gi, "*******");
i = i.replace(/\bwordtwo\b/gi, "*******");

The g means that it replaces all instances with asterisks.
The i means that it’s case insensitive, so it even filters stuff LiKe tHiS.
The \b means that it only selects it if it’s the whole word, which is useful for the aformentioned synonym.

1 Like