discord js – so im making this code for blacklisted words and it doesnt work. it doesnt give any errors but doesnt work how to fix? if you can help ps [closed]

let blacklisted = ['blacklisted', 'right'];
    let foundInText = false;//
    for(const i in blacklisted) {
        if (message.content.toLowerCase().includes(blacklisted[i])) foundInText = true;
    }
    if (foundInText === true) {
        message.delete();
        message.channel.send('no just no');
    }

that is the code so as i have said it doesnt give any errors but just doesnt do anything if i send a blacklisted word

Answer

Your for ... in logic returns the actual value of the current blacklisted object it’s iterating over, not the array index of the current value. Because of this, your code is trying to find the value of blacklisted["blacklisted"] or blacklisted["right"], which will be undefined and always return false.

To make this work with your current code, you can instead just check whether the message content contains the value of i.

if (message.content.toLowerCase().includes(i)) foundInText = true;

However, even if it does find a blacklisted word somewhere towards the start of the list, your code will still iterate over the entire list, due to lacking a break keyword when the text is found (you don’t need to iterate the list any further if you’ve already found a match). You can massively simplify your code by using the some array function, which checks if any of the elements of an array satisfies a given condition, immediately returns true once it finds any element that satisfies the condition, or false if it doesn’t find anything.

let blacklisted = ["blacklisted", "right"]

let foundInText = blacklisted.some(blacklistWord => message.content.toLowerCase().includes(blacklistWord))

if (foundInText) { // No need for === true on boolean variables
    message.delete();
    message.channel.send('no just no');
}