首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Udacity冰淇淋问答- Javascript声明/条件

Udacity冰淇淋问答- Javascript声明/条件
EN

Stack Overflow用户
提问于 2018-05-15 07:27:11
回答 3查看 986关注 0票数 0

在下面的测试中需要一些帮助。

代码语言:javascript
复制
/*
 * 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 {
    
}

我似乎找不到一种方法,只有在满足条件的情况下,才能打印语句。此时,每当我使用条件中定义的而不是的变量时,它都会在控制台中打印出来。我想找个办法来防止这种情况。希望这一切都有意义。任何帮助都将不胜感激。谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-15 07:39:05

您正在使用像=== 'str1' || 'str2这样的条件检查,这是不正确的。

您可以显式地指定所有条件,以便了解清楚。

代码语言:javascript
复制
if ((flavor === "vanilla" || flavor === "chocolate") && (vessel === "cone" || vessel === "bowl") && (toppings === "prinkles" || toppings === "peanuts")){
票数 1
EN

Stack Overflow用户

发布于 2018-05-15 07:32:19

试着这样做:

代码语言:javascript
复制
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);
}

Codepen演示

关于includes() 论MDN的参考

票数 1
EN

Stack Overflow用户

发布于 2019-08-30 12:46:12

代码语言:javascript
复制
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);
    
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50344570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档