Running the stations on DeMorgan’s Laws, some students are getting different answers for A || B && C
One set of students are doing the OR first, the other set is doing the AND first.
Code.org answer key has the OR first, but online it looks like the order of operators is NOT, AND, then OR.
Is Code.org wrong on the key, or is this by design?
I’d say it is working as intended this has to deal with how operators work with Parenthesis to specify importance of the operation
operations will always start at the left-hand evaluation (meaning if the 1st && operator equates to false it won’t evaluate the rest since && needs both sides to be true, no point on testing if one side is already false)
to show this in action let’s go step by step in a given program
var a = false;
var b = false;
var c = true;
console.log(a && b || c);
// here a is false and b is false but c is by itself meaning
// 1 side of the || is true resulting in true
console.log(a || b && c);
// here a is false while b is also false tied to an &&
// which is true since it's evauluating b&&c along with the ||
// both sides are in fact false
by changing what values get tested by you also change the boolean result.
I’m not to familiar with the person behind these laws you can change what gets evaluated first by using () as well !(Not) simply flips a boolean evaluation
for example:
var a = false;
var b = true;
console.log(b&&(a || b))
something like this will force the || evaluation to happen first while being tied to the second half of the and statement and you can group as nexessary
Here is a picture of my truth table. Depending on which boolen expression you solve for first, it changes the outcome. There were no parentheses in the question or the exemplar. Just A || B && C.
The Code.org worksheet exemplar says the (A || B) && C is correct, but the Order of Operations I have found all over the internet say A || (B && C) should be the one that is correct.
Well if it’s a phrasing correction than I’d recommend taking it up with support since there the ones that usually handle course corrections i thought you were actually asking if CDO’s truth evaluations were faulty
1 Like
Hey @dherbert, thank you so much for posting your question to the forum! I agree with with you that A || B && C evaluates like A || (B &&C). I share this precedence table with my students and it shows && having precedence over ||. I also ran the truth table for A = true, B = true, and C = false and got the following:
So this also implies that the && is the first operation.
@varrience is correct, when problems like this come up in curriculum documents its always best to email support@code.org or use the “Rate this Lesson” button from the Unit landing page:
I went ahead and submitted “The answer key for station D is incorrect. It evaluates the || operation first in the expression A || B && C when it should evaluate the && operation first.”
If you want to copy and paste my review of the lesson I think that multiple reviews of the same lesson get higher up on the priority list an fixes come a bit faster.
Good catch on this! Thanks again for bringing this up. Discussions like this really iron out the curriculum and make it better.
Best,
-Sam
I think the problem with this interpretation is that it doesn’t consider the purpose of the conditional. The game should be over when either of the two players’ scores >= 50 and they aren’t tied. I think Code.org needs to add parentheses around the OR statement so it is evaluated first. Otherwise, Player 1 has an advantage over Player 2.
1 Like
Hi @michael.fiscus , This is a great point and such a face palm moment when I finally SAW the question in its intended context after you pointed it out. Yes, I agree that given the point of the boolean statement, the OR statement should have parentheses around it for it to make sense. So the code should read: return (player1Score >= 50 || player2Score >=50) && player1Score != player2Score;
If you would review this lesson pointing this out and the others that stumble on this thread do the same hopefully the curriculum teams amends the stations document.
1 Like