我也有 编写一系列复杂的逻辑表达式,仅当以下条件为真时打印:
如果口味设置为 Vanilla 或巧克力并且 如果容器设置为锥形或碗状,并且 如果浇头设置为洒水或花生 如果以上条件为真,则打印出:
我想要两勺 __________ 冰淇淋和 __________ __________。
所以我输入了下面的代码
var flavor = "chocolate";
var vessel = "bowl";
var toppings = "peanuts";
if (flavor === "chocolate" || flavor === "vanilla" && flavor != "strawberry" &&
vessel === "cone" ||
vessel === "bowl" && vessel != "hand" && toppings === "peanuts" ||
toppings === "sprinkles" && toppings != "walnuts") {
console.log("I'd like two scoops of " + flavor + " ice cream in a " +
vessel + " with " + toppings + " .");
}
并获得以下反馈
再试一次
进展顺利
- 你的代码应该有可变的风格
- 你的代码应该有一个变量 vessel
- 你的代码应该有一个变量 toppings
- 你的代码应该有一个 if 语句
- 你的代码应该使用逻辑表达式
- 您的代码应该适用于 flavor=vanilla、vessel=cone 和 toppings=sprinkles
- 您的代码应该适用于 flavor=vanilla、vessel=cone 和 toppings=peanuts
- 您的代码应该适用于 flavor=vanilla、vessel=bowl 和 toppings=sprinkles
- 您的代码应该适用于 flavor=vanilla、vessel=bowl 和 toppings=peanuts
- 您的代码应该适用于 flavor=chocolate、vessel=cone 和 toppings=sprinkles
- 您的代码应该适用于 flavor=chocolate、vessel=cone 和 toppings=peanuts
- 您的代码应该适用于 flavor=chocolate、vessel=bowl 和 toppings=sprinkles
- 您的代码应该适用于 flavor=chocolate、vessel=bowl 和 toppings=peanuts
- 列表项
哪里出了问题
- 当使用“草莓”作为 flavor 时,您的代码不应通过。
- 当“手”用作容器时,您的代码不应通过。
- 当“核桃”用作配料时,您的代码不应通过。
最佳答案
这应该有效。
var flavor = "chocolate";
var vessel = "bowl";
var toppings = "peanuts";
if (
(flavor === "chocolate" || flavor === "vanilla") &&
(vessel === "cone" || vessel === "bowl") &&
(toppings === "peanuts" || toppings === "sprinkles")
) {
console.log("I'd like two scoops of " +
flavor + " ice cream in a " +
vessel + " with " +
toppings + " .");
}
我们不需要指定我们不需要使用的所有口味、容器和浇头。因此,如果您将口味设置为 strawberry
,程序将查看代码的第一部分:好的,我们得到了什么?味道和巧克力一样吗?没有。接下来,味道是否等于 Vanilla 味?没有。好的,这是必需的条件,所以无需进一步检查,我将停止执行。
希望对您有所帮助。
关于javascript - 学习 JavaScript,如何仅在以下语句为真时打印出这一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47391625/