在下面的测试中需要一些帮助。
/*
* Programming Quiz: Ice Cream (3-6)
*
* Write a single if statement that logs out the message:
*
* "I'd like two scoops of __________ ice cream in a __________ with __________."
*
* ...only if:
* - flavor is "vanilla" or "chocolate"
* - vessel is "cone" or "bowl"
* - toppings is "sprinkles" or "peanuts"
*
* We're only testing the if statement and your boolean operators.
* It's okay if the output string doesn't match exactly.
*/
// change the values of `flavor`, `vessel`, and `toppings` to test your code
var flavor = "strawberry";
var vessel = "hand";
var toppings = "cookies";
// Add your code here
if (flavor === "vanilla" || "chocolate" && vessel === "cone" || "bowl" && toppings === "prinkles" || "peanuts"){
console.log("I'd like two scoops of " + flavor + " ice cream in a " + vessel + " with " + toppings + ".");
}
else {
}
我似乎找不到一种方法,只有在满足条件的情况下,才能打印语句。此时,每当我使用条件中定义的而不是的变量时,它都会在控制台中打印出来。我想找个办法来防止这种情况。希望这一切都有意义。任何帮助都将不胜感激。谢谢!
发布于 2018-05-15 07:39:05
您正在使用像=== 'str1' || 'str2这样的条件检查,这是不正确的。
您可以显式地指定所有条件,以便了解清楚。
if ((flavor === "vanilla" || flavor === "chocolate") && (vessel === "cone" || vessel === "bowl") && (toppings === "prinkles" || toppings === "peanuts")){发布于 2018-05-15 07:32:19
试着这样做:
var flavor = "vanilla";
var vessel = "bowl";
var toppings = "peanuts";
if (
["vanilla","chocolate"].includes(flavor)
&& ["cone", "bowl"].includes(vessel)
&& ["sprinkles", "peanuts"].includes(toppings)) {
console.log("I'd like two scoops of %s ice cream in a %s with %s toppings.",
flavor, vessel, toppings);
}关于includes() 论MDN的参考
发布于 2019-08-30 12:46:12
var flavor = "chocolate"||"vanilla";
var vessel = "bowl"||"cone";
var toppings = "sprinkles"||"peanuts";
if(( flavor==="chocolate"|| flavor==="vanilla" ) && (vessel==="bowl"||vessel==="cone") && (toppings==="peanuts" ||toppings==="sprinkles")){
console.log("I'd like two scoops of " + flavor + " ice cream in a " + vessel + " with " + toppings);
}
https://stackoverflow.com/questions/50344570
复制相似问题