Error when trying to identify if a string is equal to one of the values of an array

Asked

Viewed 28 times

0

const Discord = require('discord.js');

module.exports.run = (client, msg, args) => {
    msg.delete();
    let type = args[0];
    let types = ['available', 'idle', 'dnd', 'invisible']
    if(!type === types[0] || types[1] || types[2] || types[3]) return msg.channel.send(new Discord.RichEmbed().setAuthor('Error').setDescription('Sintaxe errada.'))

}

Basically, when I run the command wrong, embed is sent, but when I run it right, it still embed is sent.
Someone to help? :)

  • As its name says this is Stackoverflow in Portuguese, translate your question or post it on the international website.

  • Sorry... I’ll change it.

  • There is no comparison of the type variavel === 'A' || 'B'. The answer given below is correct, but if you need to make a if of the kind, should make variavel === 'A' || variavel === 'B'

1 answer

1


To check whether a string is contained in a array you can use the function includes of array:

...
if (types.includes(type))...
...

includes

The method includes() determines whether a array contains a certain element, returning true or false appropriately.

  • I knew that, but I hadn’t thought to use it in this context. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.