logical operators java
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."
Than I've wrote those two script versions but any of that is right according the course:
var chocolate; var strawberry; var stracciatella; var caramel; var iceCream = ((strawberry && stracciatella != (caramel)) || (strawberry && chocolate != (caramel)) || (chocolate && stracciatella != (caramel)));
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.
Precedence with logical operators:
- NOT: !
- Comparison Operators: ===, >, <, >=, <=
- AND: &&
- OR: ||
If we apply this precedence to the statment "with strawberry and chocolate or stracciatella", then iceCream should have this assignment.
var iceCream = ((strawberry && chocolate) || stracciatella) || !caramel;
But the answer is not correct. The hint is...
Did you consider strawberry and stracciatella?
Here's an example that returns true based on what you've pipped. But if you wanted I could show you a function that will let only the perfumes of ice wanted.
const iceFlavour = ['chocolate', 'strawberry', 'stracciatella','caramel'] var iceCream = (((iceFlavour && iceFlavour) !== 'caramel') || ((iceFlavour && iceFlavour) !== 'caramel') || (iceFlavour || iceFlavour || iceFlavour !== '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.
It should be "&& !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".
Hope that can give you some direction.
You can read more about logical operators here:
- Your variables have no values?
- All of your flavor variables are undefined.
- First: use spaces. Second: how does the test case look like? Why all your variables are
- What I suspect you're supposed to do is write a function that accepts those flavors as boolean arguments, and THEN your expression at the end will return true or false appropriately
- @T.J.Crowder sorry, yes I didn't mean to imply that the expression is literally the correct answer, just that having the context of a function with arguments will make the expression at least make sense, and will then be open for adjustment to match the criteria.
- ita was wrong because I've tried as && !caramel and the correct is || !caramel. Thanks Tom! var chocolate = true; var strawberry = true; var stracciatella = true; var caramel = false; var iceCream = ((strawberry && chocolate) || (strawberry && stracciatella) || !caramel);