将多个变量与 JavaScript 的逻辑运算符进行比较
Comparing multiple variables with logical operators for JavaScript
我开始介绍 JavaScript 课程,我们将复习逻辑运算符。我的脚本的目标是在满足几个条件时打印一条语句。
我有 3 个变量(例如 x、y、z),如果 x = a || 我需要它打印到控制台b 和 y = c || d 和 z = e || f.
我的代码是:
var flavor = "strawberry";
var vessel = "cone";
var toppings = "cookies";
if (flavor === "vanilla" || "chocolate" && vessel === "cone" || "bowl" && toppings === "sprinkles" || "peanuts") {
console.log("I'd like two scoops of " + flavor + "ice cream in a " + vessel + "with " + toppings + ".");
} else {
console.log("No ice cream for you.");
}
它需要有香草或巧克力 && 甜筒或碗 && 洒水或花生才能打印出来。使用我的代码,它会打印变量中的任何值,无论它们是什么。
我的代码有语法错误吗?或者你不能在一个陈述中比较那么多东西吗?正如我所说,这是一门入门课程,所以我无法想象开始时会那么复杂。我的脑子里有什么东西没有开火。哈哈
任何 help/explanations 将不胜感激。
提前致谢!!
有一些规则描述了如何将多个比较链接在一起。
这些规则被称为 precedence 规则,但使用额外的括号将比较分组在一起通常更容易,这样您就不必太担心优先规则。这是带有正确括号的 if 语句:
if ((flavor === "vanilla" || flavor === "chocolate") && (vessel === "cone" || vessel === "bowl") && (toppings === "sprinkles" || toppings === "peanuts"))
问题在于您如何使用 OR 条件。在 JS 中,当您使用不同于 undefined
或 null
或 0
,或 ""
,或 NaN
的对象时,条件 returns true
.
所以,你需要改变它。基本上,如果您需要对同一个变量进行多次比较,请执行以下操作:
var flavor = "strawberry";
var vessel = "cone";
var toppings = "cookies";
if ((flavor === "vanilla" || flavor === "chocolate") && (vessel === "cone" || vessel === "bowl") && (toppings === "sprinkles" || toppings === "peanuts")) {
console.log("I'd like two scoops of " + flavor + "ice cream in a " + vessel + "with " + toppings + ".");
} else {
console.log("No ice cream for you.");
}
或者,您可以将数组与函数一起使用 includes
。
var flavor = "strawberry";
var vessel = "cone";
var toppings = "cookies";
if (["vanilla", "chocolate"].includes(flavor) && ["cone", "bowl"].includes(vessel) && ["sprinkles", "peanuts"].includes(toppings)) {
console.log("I'd like two scoops of " + flavor + "ice cream in a " + vessel + "with " + toppings + ".");
} else {
console.log("No ice cream for you.");
}
我开始介绍 JavaScript 课程,我们将复习逻辑运算符。我的脚本的目标是在满足几个条件时打印一条语句。
我有 3 个变量(例如 x、y、z),如果 x = a || 我需要它打印到控制台b 和 y = c || d 和 z = e || f.
我的代码是:
var flavor = "strawberry";
var vessel = "cone";
var toppings = "cookies";
if (flavor === "vanilla" || "chocolate" && vessel === "cone" || "bowl" && toppings === "sprinkles" || "peanuts") {
console.log("I'd like two scoops of " + flavor + "ice cream in a " + vessel + "with " + toppings + ".");
} else {
console.log("No ice cream for you.");
}
它需要有香草或巧克力 && 甜筒或碗 && 洒水或花生才能打印出来。使用我的代码,它会打印变量中的任何值,无论它们是什么。
我的代码有语法错误吗?或者你不能在一个陈述中比较那么多东西吗?正如我所说,这是一门入门课程,所以我无法想象开始时会那么复杂。我的脑子里有什么东西没有开火。哈哈
任何 help/explanations 将不胜感激。
提前致谢!!
有一些规则描述了如何将多个比较链接在一起。
这些规则被称为 precedence 规则,但使用额外的括号将比较分组在一起通常更容易,这样您就不必太担心优先规则。这是带有正确括号的 if 语句:
if ((flavor === "vanilla" || flavor === "chocolate") && (vessel === "cone" || vessel === "bowl") && (toppings === "sprinkles" || toppings === "peanuts"))
问题在于您如何使用 OR 条件。在 JS 中,当您使用不同于 undefined
或 null
或 0
,或 ""
,或 NaN
的对象时,条件 returns true
.
所以,你需要改变它。基本上,如果您需要对同一个变量进行多次比较,请执行以下操作:
var flavor = "strawberry";
var vessel = "cone";
var toppings = "cookies";
if ((flavor === "vanilla" || flavor === "chocolate") && (vessel === "cone" || vessel === "bowl") && (toppings === "sprinkles" || toppings === "peanuts")) {
console.log("I'd like two scoops of " + flavor + "ice cream in a " + vessel + "with " + toppings + ".");
} else {
console.log("No ice cream for you.");
}
或者,您可以将数组与函数一起使用 includes
。
var flavor = "strawberry";
var vessel = "cone";
var toppings = "cookies";
if (["vanilla", "chocolate"].includes(flavor) && ["cone", "bowl"].includes(vessel) && ["sprinkles", "peanuts"].includes(toppings)) {
console.log("I'd like two scoops of " + flavor + "ice cream in a " + vessel + "with " + toppings + ".");
} else {
console.log("No ice cream for you.");
}