javascript - Udacity 冰淇淋测验 - Javascript 语句/条件

标签 javascript variables boolean

需要一些帮助来完成下面的测验。

/*
 * 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 {
    
}

我似乎找不到如何仅在满足条件时打印语句的方法。此时,每当我使用条件中定义的变量时;无论如何它都会在控制台中打印出来。 我想找到一种方法来防止这种情况发生。 希望这一切都有道理。任何帮助将非常感激。 谢谢!

最佳答案

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

您可以明确指定所有条件,以便于理解。

if ((flavor === "vanilla" || flavor === "chocolate") && (vessel === "cone" || vessel === "bowl") && (toppings === "prinkles" || toppings === "peanuts")){

关于javascript - Udacity 冰淇淋测验 - Javascript 语句/条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50344570/

相关文章:

JavaScript 没有函数的变量提升

c++ - 试图获取从文件中读取的随机素数之和?

php - 将字符串 'true' 或 'false' 转换为整数 '1' 或 '0'

variables - 有没有一种方法可以将颜色十六进制代码存储在类似于 React-Native 中的 SASS 的变量中?

Android - 具有 boolean 错误的 ContentValue 数据库?

java - 如果成员变量关闭,如何在不将类型从 void 更改为 boolean 的情况下显示 true?

javascript - Backbone 集合获取导入不正确

javascript - 在javascript中获取 session 数组的值

javascript - array.push(object) 未在 javascript 中的循环末尾推送

javascript - jQuery 简单可折叠 Div?