Javascript 开关中的If语句

Javascript 开关中的If语句,javascript,if-statement,Javascript,If Statement,我正在为学习javascript的课程准备一个问题。我在尝试在交换机中添加if语句时遇到了问题。我目前有: var user = prompt("Are you ready for battle?!").toUpperCase(); switch(user) { case'YES': if(YES && NO) { console.log("Great, let's do it."); } else {

我正在为学习javascript的课程准备一个问题。我在尝试在交换机中添加if语句时遇到了问题。我目前有:

var user = prompt("Are you ready for battle?!").toUpperCase();
switch(user) {
    case'YES':
        if(YES && NO) {
           console.log("Great, let's do it.");
        } else {
            console.log("Bye");
        }
        console.log("Great! It will be a long exciting battle.");
        break;  
    case'NO':
        console.log("Come back when you are ready.");
        break;
    case'MAYBE':
        console.log("Go away. This is only for the brave");
        break;
        default:
        console.log("You obviously do not belong here. It was a simple yes/no question.")
}
问题是:

Add some if/else statements to your cases that check to see whether one
condition and another condition are true, as well as whether one condition 
or another condition are true. Use && and || at least one time each.
我得到的错误是:ReferenceError:YES未定义


我可以在if的条件中添加什么来实现这一点,或者如何定义“是”?

在这里,您似乎遇到了两个问题

首先是评论中指出的问题,您将是和否视为变量,而事实并非如此。为了避免通过提供代码的更正版本而剥夺您学习的机会,我只给出相关示例

var word = "test";
// If we compare against the string literally, it will have an error
// because it's looking for a variable with that name.
if (word === test) { } // ReferenceError: test is not defined

// we fix this by quoting what we're comparing against
if (word === "test") { } // Code inside the block would be executed :)

// But what about checking the value of "test" by itself?
// A string is "truthy", meaning that it passes the conditional test always.
if ("test") { } // Code would be executed always regardless of the content of var word

// Stringing multiple constants together doesn't make sense
if ("test" && "word") { } // This is equivalent...
if (true && true) { } // ... to this, which doesn't make sense
这就引出了你要解决的第二个问题。问题的要求指定检查一个条件和另一个条件是否为真,以及一个条件和另一个条件是否为真。问题是您只需要检查一个条件:变量用户的状态

只有当你不知道某件东西是什么的时候,测试它的状态才有意义。从用户收到的输入就是一个完美的例子。因此,我建议您从用户那里获取更多信息,例如姓名、年龄、性别、鞋号或其他信息。然后,您可以检查以下条件:

// You would need to store user input in variables username and age previously... 
if (username === "vastlysuperiorman" && age < 13) { console.log("You're awfully young!"); }

// Or to only allow people within an age range... 
if (age < 13 || age > 31) { console.log("You must be between 13 and 31 years old to play this game!"); }

一旦有多个条件要检查,就可以在函数、case语句和其他if语句中的任意位置检查它们。没关系。只需添加一个if块并测试条件:

YES应该是YES,NO也应该是YES。但不确定你所做的是否真的有意义。错误告诉你什么是错的,YES没有定义。对于这个问题,您没有名为YES或no的变量。如果您的意思是user==YES,那么这是多余的,因为您的交换机已经处理了它;如果您的意思是user==YES&&user==NO,那么这是没有意义的,因为它总是错误的。user可以是YES,也可以是nothing。这个交换机有很多错误。我认为您需要重新检查代码背后的逻辑。您可能应该接受更多的输入。。。您不能检查多个条件,因为您只有一个输入。例如,询问年龄、性别和你是否准备好了。然后用开关来处理是,不是,也许。在每种情况下,你都可以做一些事情,比如if age>13&&gender==f{console.logYou rock,girl!;}。@SterlingArcher:我想说,在if的条件下,我能做的事情并不是一个明确的问题陈述。工作意味着什么?代码应该做什么?我们都可以看到眼前的问题是的,没有定义,但无论如何,这肯定不是我们需要的。