0

I am trying to make a profanity filter. I have an array of bad words and if a user enters a message containing a bad word I want it to flag that message. I got everything working, but the filter also flags words that include parts of a bad word. Like 'Glass' containing the word 'ass'. I know I used the 'includes' method. But I want it to only become true if it is an exact match to a word in the filter array.

for (var i = 0; i < ProfanityFilter.length; i++) 
   if (message.toLowerCase().includes(ProfanityFilter[i])) 

Is there a way to use something else?

2
  • Use a regular expression to match the bad words. Commented May 22, 2020 at 2:46
  • How would I do that when I have an array of strings and not a single string? Commented May 22, 2020 at 2:56

2 Answers 2

2

You can use Array.some. It will check your message for every entry in ProfanityFilter, If found it will break the loop and return true else false.

const ProfanityFilter = ['ass', 'cat']
const message = 'bass'
const shouldFilter = ProfanityFilter.some(x => x === message.toLowerCase())
console.log(shouldFilter)

const message2 = 'ass'
const shouldFilter2 = ProfanityFilter.some(x => x === message2.toLowerCase())
console.log(shouldFilter2)

Sign up to request clarification or add additional context in comments.

Comments

1

Make use of the array.some method as so:

const ProfanityFilter = ["fart", "ass", "mad"]

const message1 = "Good words only"
const split1 = message1.split(" ")

const check = each => (
  ProfanityFilter.every(word => word !== each.toLowerCase()) === true
)

const filter1 = split1.every(check)

console.log((filter1) && ("don't delete message: " + message1))


const message2 = "I am mad at you"
const split2 = message2.split(" ")

const check2 = each => (
  ProfanityFilter.every(word => word !== each.toLowerCase()) === true
)

const filter2 = split2.every(check2)

function findBadWord(filter, split) {
  const objMap = {};
  filter.forEach((e1) => split.forEach((e2) => {
    if (e1 === e2) {
      objMap[e1] = objMap[e1] + 1 || 1;
    }
  }));
  return(Object.keys(objMap).map(e => e));
}


console.log((!filter2) && ("delete message, it contains the bad word(s): " + findBadWord(ProfanityFilter, split2)))

6 Comments

If I then wanted to include in the console.log what word it was. What should I do?
If you run the code snippet, you can see the defaulting or non-defaulting word is appended to the end of the console.log output. I made use of the good ol' + then variable to display the word.
Okay, so this only checks if that word is the only thing in the string, but what if I want to check if the word is present in the sentence aswell? Since if you put a profanity word in a sentence with this method. Then it will come out false
So are you asking for 3 scenarios? 1. when it contains the word, 2. when it doesn't contain any bad word 3. when the message itself is a bad word?. Considering your edit, then you are broadening the scope of your initial question, your initial question is based on a user entering "A WORD" and checking if that word is profane or not, obviously the code would change a bit, for inputs of more than one word.
So. I need an if statement that can check a message to see if it contains any of the words in the array of words. If that word is present somewhere in the message it will get flagged. But it has have the exact word present in the sentence.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.