[英]JavaScript logical operators questions?
I just don't understand how && and || 我只是不明白&&和||的方式 work.
工作。 I wrote up a small code to help myself out and it just doesn't make any sense to me.
我写了一个小代码来帮助自己,对我来说这毫无意义。 You would click a button and the startGame() would be called.
您将单击一个按钮,然后将调用startGame()。
var startGame = function() {
var quizAnswers = {
name: prompt("What is your name?").toUpperCase(),
age: prompt("What is your age?"),
snack: prompt("What is your favorite type of snack out of the following: ice cream, apple, chips, cookies?").toUpperCase()
};
quizAnswers.confirmAge = function () {
while (isNaN(this.age) === true) {
this.age = prompt("The age that you entered- " + this.age + " -is not a number. Please enter a number.");
};
};
quizAnswers.confirmAge();
quizAnswers.confirmSnack = function () {
while ((this.snack !== "ICE CREAM") && (this.snack !== "APPLE") && (this.snack !== "CHIPS") && (this.snack !== "COOKIES")) {
this.snack = prompt("The snack you entered- " + this.snack + " -is unrecognized. Please enter: ice cream, apple, chips, or cookies.").toUpperCase();
};
};
quizAnswers.confirmSnack();
It would get name, age, and favorite snack and then check to see if the age is a number and the snack entered is one of the listed options. 它会显示名称,年龄和最喜欢的小吃,然后检查年龄是否为数字,输入的小吃是否为所列选项之一。 After messing with the while loop in the confirmSnack function, I figured out how to make it work, which is displayed above.
在搞砸了ConfirmSnack函数中的while循环之后,我弄清楚了如何使其工作,如上所示。 But why is it && and not ||.
但是为什么&&而不是||。 And is there a way to shorten it like:
并有一种缩短它的方法,例如:
while (this.snack !== ("ICE CREAM" && "APPLE" && "CHIPS" && "COOKIES")) {
this.snack = prompt("The snack you entered- " + this.snack + " -is invalid. Please enter: ice cream, apple, chips, or cookies.").toUpperCase();
};
So the questions are to explain why &&(and) is used instead of ||(or) and if there is a way to shorten this code so i don't have to enter "this.snack !==" four times. 因此,问题是要解释为什么使用&&(and)而不是||(or),以及是否有一种方法可以缩短此代码,因此我不必四次输入“ this.snack!==“。 I'm not an expert so please try to keep it simple.
我不是专家,因此请尽量保持简单。
The && operator is working just fine. &&运算符工作正常。 This is actually a question of logic, not javascript.
这实际上是逻辑问题,而不是JavaScript。
You are asking the question while the answer is different from ALL of the possible answers. 您在询问问题时答案与所有可能的答案都不相同。
It could be rewritten with || 可以用||重写 as the follwing:
如下:
while (!(this.snack == "ICE CREAM" || this.snack == "APPLE" || this.snack == "CHIPS" || this.snack == "COOKIES"))
Notice the !
注意
!
operator in the beginning. 运算符开头。
A shorter form to write it would be: 一个简短的形式是:
answers = ["ICE CREAM", "APPLE", "CHIPS", "COOKIES"];
while (answers.indexOf(this.snack) < 0) { ... }
Here you define a list of possible answers and you want to accept, and check if the answer is among them. 在这里,您可以定义一个可能的答案列表,并且要接受,并检查答案是否在其中。
The &&
and ||
&&
和||
operators compare boolean values. 运算符比较布尔值。 (Boolean means true/false).
(布尔值表示是/否)。 That means if you execute
5 == 5 && 6 + 1 == 7
the interpreter does the following things: 这意味着,如果执行
5 == 5 && 6 + 1 == 7
则解释器将执行以下操作:
Evaluate 5 == 5
. 评估
5 == 5
。 The ==
operator returns true if both sides are equal (as you probably know). 如果两边相等,则
==
运算符将返回true(您可能知道)。 Since 5 == 5
is true
, we look at the next value (if it were false, because of short circuit operators, it would return false immediately). 由于
5 == 5
为true
,因此我们查看下一个值(如果为假,由于短路运算符,它将立即返回假)。
Evaluate 6 + 1 == 7
. 评估
6 + 1 == 7
。 This is also true, so the returned value is true
. 这也是正确的,因此返回值是
true
。
The &&
operator does not compare regular values such as "ICE CREAM"
(well it does, but it converts it into a boolean value). &&
运算符不会比较"ICE CREAM"
类的常规值(可以,但是会将其转换为布尔值)。
Now let's look at the code you provided: 现在,让我们看一下您提供的代码:
this.snack !== ("ICE CREAM" && "APPLE" && "CHIPS" && "COOKIES")
First, the javascript interpreter executes ("ICE CREAM" && "APPLE" && "CHIPS" && "COOKIES")
. 首先,执行javascript解释器
("ICE CREAM" && "APPLE" && "CHIPS" && "COOKIES")
。 Since all of those values are true, the value of that expression is true
. 由于所有这些值均为true,因此该表达式的值为
true
。 So this comparison is essentially checking this.snack !== true
, which is not what you want. 因此,此比较实质上是检查
this.snack !== true
,这不是您想要的。
To solve your problem, I would suggest using indexOf
. 为了解决您的问题,我建议使用
indexOf
。 This checks if an element is in an array and returns -1
if there is no element. 这将检查元素是否在数组中,如果没有元素,则返回
-1
。 For example: 例如:
var validSnacks = ["ICE CREAM", "APPLE", "CHIPS", "COOKIES"];
while (validSnacks.indexOf(this.snack) === -1) {
// Do something
}
If you use or(||)
, if any of the sub-statements (eg this.snack !== "ICE CREAM") evaluate to true
, the entire statement will be true, causing the prompt to be shown. 如果使用
or(||)
,则任何子语句(例如this.snack!==“ ICE CREAM”)的评估结果为true
,则整个语句将为true,从而显示提示。
On the other hand, using and(&&)
, all of the sub-statements must be true for the entire statement to evaluate as true, which is the behaviour that you want - you only want the prompt to show if snack is not one of the 4 options. 另一方面,使用
and(&&)
, 所有子语句必须为true,整个语句才能评估为true,这是您想要的行为-您只希望提示显示零食是否不是其中之一4个选项。
There is no way to shorten the code in the way you suggest. 无法按照您建议的方式缩短代码。 One thing you could do would be to make an array with the 4 options in it and check if that array contains snack.
您可以做的一件事是制作一个带有4个选项的阵列,并检查该阵列是否包含零食。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.