DeMorgan's laws. Insane... (JavaScript)

ibex333

Diamond Member
Mar 26, 2005
4,094
123
106
Let me give you two statements:

1)As long as the answer is not x OR not y, do something.
2)As long as the answer is not x AND y, do something.

In the first scenario, that something STILL gets done even if the answer is x or y...

But I just said "do it ONLY if answer is x or y" !!!! What gives? This is driving me nuts!



In the second case as long as the answer is x or y, something will be done as intended. But it SHOULDN'T, because the statement says "do it ONLY if BOTH x AND y are the answer TOGETHER, AT THE SAME TIME"!


My code: (wrong)


while (natBornCitizen !== 'Y'
|| natBornCitizen !== 'y'
|| natBornCitizen !== 'Yes'
|| natBornCitizen !=='yes'
|| natBornCitizen !== 'N'
|| natBornCitizen !== 'n'
|| natBornCitizen !== 'No'
|| natBornCitizen != 'no');
{ alert }


someone else's code that "fixed" mine: (correct)

while (natBornCitizen !== 'Y'
&& natBornCitizen !== 'y'
&& natBornCitizen !== 'Yes'
&& natBornCitizen !=='yes'
&& natBornCitizen !== 'N'
&& natBornCitizen !== 'n'
&& natBornCitizen !== 'No'
&& natBornCitizen != 'no');
{ alert }




And yet, I still dont understand this....



EDIT: Hmm... I think I just understood this by writing this post...

As long as the answer is not x OR not y, do something. this is the same as saying as long as the answer is x and y do something... But than again, who negated the OR to AND? We only negated not x, not y!
 
Last edited:

iCyborg

Golden Member
Aug 8, 2008
1,388
94
91
In the first scenario, that something STILL gets done even if the answer is x or y...
This is incorrect: if both x and y are true, then (x or y) is still true, but not x or not y is false, so we shouldn't be doing something.

1) not x or not y - this means either x is false, or y is false, or both are false
2) not (x and y) - this means it's not true that both x and y are true

If it isn't obvious to you that these say the same thing, then I don't know, perhaps just work out via truth tables and accept it as a fact.

As long as the answer is not x OR not y, do something. this is the same as saying as long as the answer is not( x and y ) do something... But than again, who negated the OR to AND? We only negated not x, not y!
Fixed it for you. OR gets negated to AND when you pull not out, the above gives the common sense reason, and if you don't agree, then simply accept De Morgan's laws as rules of logic, which can be proven via truth tables to be identical


Look at your code: your condition can never be false, it's always true. In order for it to be false, natBornCitizen would have to be equal to 'Y', 'y', Yes' ..., all those at the same time, which it clearly can't be. E.g. if natBornCitizen is 'Y', then it's not equal to 'y', so the second statement is true, and since you're OR-ing, the whole condition is true. You're always alerting.
The correct code is false if all those are true, so natBornCitizen is not equal to any of those, which is probably the intended meaning.
 

fread2281

Member
Sep 27, 2012
32
0
0
Think of it like
x !== z || x !== q -> true || true -> false
x !== z && x !== q -> true && true -> true
 

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
2 is just the simple way of saying 1.

if not (x && y) then....

Or in otherwords, (x != 1) && (y != 1) assuming bits are only being used.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,816
75
If you like logical statements in English, then you might want to look up the contrapositive. I think it's similar, though there's an inequality involved.

If you do not want to look up the contrapositive, then you might not like logical statements in English. ;)
 

uclabachelor

Senior member
Nov 9, 2009
448
0
71
Let me give you two statements:

1)As long as the answer is not x OR not y, do something.
2)As long as the answer is not x AND y, do something.

In the first scenario, that something STILL gets done even if the answer is x or y...

But I just said "do it ONLY if answer is x or y" !!!! What gives? This is driving me nuts!



In the second case as long as the answer is x or y, something will be done as intended. But it SHOULDN'T, because the statement says "do it ONLY if BOTH x AND y are the answer TOGETHER, AT THE SAME TIME"!


My code: (wrong)


while (natBornCitizen !== 'Y'
|| natBornCitizen !== 'y'
|| natBornCitizen !== 'Yes'
|| natBornCitizen !=='yes'
|| natBornCitizen !== 'N'
|| natBornCitizen !== 'n'
|| natBornCitizen !== 'No'
|| natBornCitizen != 'no');
{ alert }


someone else's code that "fixed" mine: (correct)

while (natBornCitizen !== 'Y'
&& natBornCitizen !== 'y'
&& natBornCitizen !== 'Yes'
&& natBornCitizen !=='yes'
&& natBornCitizen !== 'N'
&& natBornCitizen !== 'n'
&& natBornCitizen !== 'No'
&& natBornCitizen != 'no');
{ alert }




And yet, I still dont understand this....



EDIT: Hmm... I think I just understood this by writing this post...

As long as the answer is not x OR not y, do something. this is the same as saying as long as the answer is x and y do something... But than again, who negated the OR to AND? We only negated not x, not y!

Draw a Karnaugh map and the answer will be obvious.
 

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
Maybe I didn't think heavily enough on this when I learned it..but

I always just flipped the signs and the and/or when negating

!(!x or !y) = x and y

also

char natBorn = natBornCitizen.toLower().charAt(0);

while (natBorn !== 'y' && natBorn !== 'n')
{ alert }

really isn't "better" but a lot less cluttered