或逻辑运算符无法正常运行javascript

1AndOnlyPika

我的代码当前将检查输入是否有效,而我使用的是or运算符。

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!")
                   }
                   }
                 })
            }

但是当这样做时,由于某种原因它不能识别/工作,即使我输入值xmr,它也会执行我定义的“其他”。如果有人知道我对or运算符做错了,请告诉我

防区

您在条件之间使用了逻辑或运算符。也就是说,您说:

返回错误信息,

  • 如果tosell不是xmrOR
  • 如果tosell不是ethOR
  • 如果tosell不是btc

这将返回ALWAYS true,因为值不能等于xmreth并且btc同时,因此它将始终返回错误消息。

使用逻辑AND运算符:

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

或使用一个否定的Array#includes@Jack建议的相反):

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

逻辑运算符在 jQuery 中无法正常工作

来自分类Dev

JavaScript逻辑运算符&& in语句?

来自分类Dev

Javascript“ If”语句或“ &&”逻辑运算符

来自分类Dev

Javascript逻辑运算符或

来自分类Dev

Javascript逻辑运算符混乱

来自分类Dev

逻辑运算符

来自分类Dev

无法理解这个逻辑运算符的行为

来自分类Dev

无法理解这个逻辑运算符的行为

来自分类Dev

逻辑运算符无法在php中工作

来自分类Dev

如何使用逻辑运算符JavaScript或jQuery

来自分类Dev

逻辑运算符及其在JavaScript中的行为

来自分类Dev

JavaScript逻辑运算符和赋值?

来自分类Dev

如何返回AND和OR逻辑运算符JavaScript

来自分类Dev

在条件中使用逻辑运算符和比较运算符(javascript)

来自分类Dev

在JavaScript中简单替换逻辑运算符的赋值运算符?

来自分类Dev

PHP多重逻辑运算符

来自分类Dev

反向逻辑运算符AND(&)

来自分类Dev

指针不是逻辑运算符

来自分类Dev

逻辑运算符或无短路

来自分类Dev

逻辑运算符的混乱

来自分类Dev

C逻辑运算符的差异

来自分类Dev

awk逻辑运算符

来自分类Dev

bash中的OR逻辑运算符

来自分类Dev

了解XOR逻辑运算符

来自分类Dev

逻辑运算符AND(&)的反函数

来自分类Dev

逻辑运算符是jQuery

来自分类Dev

方案-逻辑运算符程序

来自分类Dev

JS split OR逻辑运算符

来自分类Dev

逻辑运算符的行为如何?