简体   繁体   中英

or logical operator not working javascript

My code will currently check if the input is valid or not, and I am using an or operator to do so.

if (tosell !== 'xmr' || tosell !== 'eth' || tosell !== 'btc') {
              message.channel.send('Unknown currency. Valid currencies are: xmr, eth, btc. Example: $sell xmr')
            } else {
              message.channel.send('How much '+tosell+' would you like to sell?')
              const collector = new Discord.MessageCollector(message.channel, m => m.author.id === message.author.id, { time: 5000 });
                collector.on('collect', async message => {
                 amount = parseInt(message.content)
                 if (amount === 'all') {
                   realamount = score.xmr
                 } else if (amount != 'all') {
                   console.log(score.xmr)
                   realamount = amount
                 }
                 if (tosell === 'xmr') {
                   if (userscore.xmr >= amount) {
                     var cg = await fetch('https://api.coingecko.com/api/v3/coins/monero?tickers=true&market_data=true&community_data=false&developer_data=false&sparkline=false').then(response => response.json());;
                     const xmrprice = cg.market_data.current_price.usd
                     const toAdd = realamount*xmrprice
                      userscore.xmr -= amount
                      userscore.usd += toAdd
                      const embed = new Discord.MessageEmbed()
                          .setColor('#7FDB7F')
                          .setTitle('You sold '+amount+'XMR for $'+toAdd)
                          .setAuthor('Successful Sell', 'https://cdn.discordapp.com/emojis/710590499991322714.png?v=1')
                          message.channel.send(embed)
                      client.setScore.run(userscore);
                   } else {
                     message.channel.send("You don't have enough Monero to sell!")
                   }
                   }
                 })
            }

But when it does that it doesnt recognize/work for some reason and even if I am entering the value xmr it does the 'else' I've defined. If someone knows what I'm doing wrong with the or operators please tell me

Because it's !== - that's "not equal". Use === for equal:

if (tosell === 'xmr' || tosell === 'eth' || tosell === 'btc')

Also you can use includes for more concise syntax:

if (['xmr', 'eth', 'btc'].includes(tosell))

You've used logical OR operators between the conditions. That is, you say:

Return error message,

  • if tosell is not xmr OR
  • if tosell is not eth OR
  • if tosell is not btc .

That will return ALWAYS true , as a value can't be equal to xmr , eth and btc at the same time, so it will always return the error message.

Use logical AND operators:

if (tosell !== 'xmr' && tosell !== 'eth' && tosell !== 'btc') {

Or use a negated Array#includes (the inverse of what @Jack proposed):

if (!['xmr', 'eth', 'btc'].includes(tosell)) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM