Javascript逻辑运算符问题

时间:2017-09-20 20:35:09

标签: javascript logical-operators

我正在学习逻辑运算符,我对此代码有疑问。它在变量容器和味道中完美地工作。但是当涉及到浇头时,我的代码显示除了if语句之外的每个字符串。为什么会发生这种情况?我该如何解决?

/*
 * 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"
 */

// change the values of `flavor`, `vessel`, and `toppings` to test your code
var flavor = "chocolate";
var vessel = "cone";
var toppings = "walnuts";

// Add your code here


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 + ".");
}

0 个答案:

没有答案