简体   繁体   English

JavaScript 练习逻辑运算符(不起作用)

[英]JavaScript Exercise Logical Operators (doesn't work)

I'm doing a JavaScript course and there's a exercise about logical operators that's doesn't work right.我正在上 JavaScript 课程,并且有一个关于逻辑运算符的练习,它不能正常工作。 The question is:问题是:

Using the logical operators you just learned, write an expression that represents the following statement: "I would like an ice cream with strawberry and chocolate or stracciatella, or an ice cream of any flavour but caramel."使用您刚刚学习的逻辑运算符,编写一个表示以下语句的表达式:“我想要一个草莓和巧克力或 stracciatella 冰淇淋,或者除了焦糖以外的任何口味的冰淇淋。”

Than I've wrote those two script versions but any of that is right according the course:比我写的这两个脚本版本,但根据课程,其中任何一个都是正确的:

VERSION 1:版本 1:

var chocolate;
var strawberry;
var stracciatella;
var caramel;
var iceCream = ((strawberry && stracciatella != (caramel)) || (strawberry && chocolate != (caramel)) || (chocolate && stracciatella != (caramel)));

VERSION 2:版本 2:

var chocolate;
var strawberry;
var stracciatella;
var caramel;
var iceCream = ((strawberry && stracciatella) || (strawberry && chocolate) || (chocolate && stracciatella) != (caramel));

I really don't know whats going wrong buts.我真的不知道出了什么问题,但是。 Maybe I didn't interpreted the question right, so if someone in community help me to solve this I'll be very thankful.也许我没有正确解释这个问题,所以如果社区中有人帮助我解决这个问题,我将非常感激。

Here's an example that returns true based on what you've pipped.这是一个示例,它根据您所获取的内容返回 true。 But if you wanted I could show you a function that will let only the perfumes of ice wanted.但如果你愿意,我可以给你看一个 function,它只会让你想要的冰香。

 const iceFlavour = ['chocolate', 'strawberry', 'stracciatella','caramel'] var iceCream = (((iceFlavour[1] && iceFlavour[2]).== 'caramel') || ((iceFlavour[0] && iceFlavour[1]) !== 'caramel') || (iceFlavour[0] || iceFlavour[1] || iceFlavour[2] !== 'caramel')) console.log(iceCream)

Firstly you are saying ".= caramel" which is not a logical operator but a comparison operator so "!= caramel" will not work in the context of the task.首先,您说“.= caramel”不是逻辑运算符而是比较运算符,因此“!= caramel”在任务上下文中不起作用。

It should be "&&.caramel".它应该是“&&.caramel”。

Also, you have not assigned any value to your variables which will make the expression result in "undefined";此外,您还没有为变量分配任何值,这将使表达式结果为“未定义”; Try giving each of them a value like "let chocolate = 'chocolate'" or a boolean like "let chocolate = true" and "let caramel = false".尝试给它们中的每一个赋予一个值,例如“let Chocolate = 'chocolate'”或 boolean,例如“let Chocolate = true”和“let caramel = false”。

Hope that can give you some direction.希望能给你一些方向。

You can read more about logical operators here:您可以在此处阅读有关逻辑运算符的更多信息:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

There are a couple of issues with both the question and your code.问题和您的代码都有几个问题。 I'll tackle them one by one.我会一一解决。

First off, the question is unclear on which case it refers to.首先,这个问题不清楚它指的是哪种情况。

Case 1情况1

I would like an ice cream with (strawberry and chocolate) or stracciatella我想要一份带有(草莓和巧克力)或 stracciatella 的冰淇淋

Case 2案例2

I would like an ice cream with strawberry and (chocolate or stracciatella)我想要一份草莓冰淇淋和(巧克力或 stracciatella)

An ice cream with strawberry and stracciatella would be incorrect in case 1 but correct in case 2.带有草莓和 stracciatella 的冰淇淋在情况 1 中是不正确的,但在情况 2 中是正确的。

Now about your code, your variables are unset, more acurately "undefined" which makes them evaluate as False in any logical operations.现在关于您的代码,您的变量未设置,更准确地说是"undefined" ,这使得它们在任何逻辑操作中都评估为 False。 It looks like in that example you are supposed to set them to either true of false and then change these values around to see the outcome of your logic.在该示例中,您应该将它们设置为 true 或 false,然后更改这些值以查看逻辑的结果。

Inside your logic, you are using the != operator instead of just !在您的逻辑中,您正在使用!=运算符而不是! The difference is that you use != for comparison while you use !不同之处在于您使用!=进行比较而使用! to describe the value.来描述价值。

example 1: one != two , evaluates as true (assuming one = 1 , two = 2 )示例 1: one != two ,计算结果为true (假设one = 1two = 2

example 2: !truth , would show false (assuming truth = true )示例 2: !truth ,将显示为false (假设truth = true

So in your code you need to所以在你的代码中你需要

  • Assign values (true / false) to your variables为变量赋值(真/假)
  • Change your != operators to !将您的!=运算符更改为!

This is the fixed code, try to experiment by changing the values of the flavours to see what happens这是固定代码,尝试通过更改风味的值来进行实验,看看会发生什么

 var chocolate = true; var strawberry = false; var stracciatella = true; var caramel = true; // case 1 var icecream = ((strawberry && chocolate) || stracciatella) ||;caramel. console:log("case 1;" + icecream); // case 2 var icecream = (strawberry && (chocolate || stracciatella)) ||.caramel: console;log("case 2:" + icecream);

I would suggest to take a look at how Python logical operators work.我建议看看 Python 逻辑运算符是如何工作的。 In python words are used instead of symbols for logical operators, so && is AND , ||在 python 中,逻辑运算符使用单词而不是符号,因此&&AND|| is OR , !OR! is NOT , != is NOT EQUAL etc. It's just more clear and easy to understand by looking at it as python uses the a minimal amount of symbols in these statements and allows for these operations to be worded as we would describe them when speaking. is NOT , != is NOT EQUAL等。通过查看它会更加清晰易懂,因为 python 在这些语句中使用了最少的符号,并允许按照我们在说话时描述它们的方式来表达这些操作。

Precedence with logical operators:逻辑运算符的优先级:

  1. NOT: !不是: !
  2. Comparison Operators: ===, >, <, >=, <=比较运算符:===、>、<、>=、<=
  3. AND: &&和: &&
  4. OR: ||或:||

If we apply this precedence to the statment "with strawberry and chocolate or stracciatella", then iceCream should have this assignment.如果我们将此优先级应用于“草莓和巧克力或 stracciatella”的陈述,那么冰淇淋应该有这个任务。

var iceCream = ((strawberry && chocolate) || stracciatella) || !caramel;

But the answer is not correct.但答案并不正确。 The hint is...提示是...

Did you consider strawberry and stracciatella?你考虑过草莓和stracciatella吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM