gpt4 book ai didi

javascript - 如何结合使用 Javascript 逻辑运算符

转载 作者:行者123 更新时间:2023-12-01 02:03:37 25 4
gpt4 key购买 nike

我试图了解如何在 Javascript 中组合逻辑运算符。例如,如果满足以下条件,我将如何打印语句

  1. 如果口味设置为 Vanilla 或巧克力,并且
  2. 如果容器设置为圆锥形或碗形且
  3. 如果配料设置为糖粉或花生

这是我写的代码,但比较是错误的,它仍然打印出一条消息

var flavor = 'vanilla';
var vessel = 'cup';
var toppings = 'peanuts';

if (flavor === 'vanilla' || 'chocolate' && vessel === 'cone' || 'bowl') {
if (toppings === 'peanuts' || 'sprinkles') {
console.log('I\'d like two scoops of ' + flavor + ' ice cream in a ' + vessel + ' with ' + toppings + '.');
}
}

结果 - I'd like two scoops of vanilla ice cream in a plate with peanuts.

我错过了什么?似乎看不出错误来自哪里。

最佳答案

这个:

flavor === 'vanilla' || 'chocolate'

无效。您必须独立测试每个条件:

(flavor === 'vanilla' || flavor === 'chocolate')

其他人也有类似的错误。

关于javascript - 如何结合使用 Javascript 逻辑运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50301703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com