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

Discussion in 'Programming' started by ibex333, Oct 28, 2012.

  1. ibex333

    ibex333 Diamond Member

    Joined:
    Mar 26, 2005
    Messages:
    3,454
    Likes Received:
    0
    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!
     
    #1 ibex333, Oct 28, 2012
    Last edited: Oct 29, 2012
  2. iCyborg

    iCyborg Golden Member

    Joined:
    Aug 8, 2008
    Messages:
    1,173
    Likes Received:
    2
    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.

    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.
     
  3. fread2281

    fread2281 Member

    Joined:
    Sep 27, 2012
    Messages:
    32
    Likes Received:
    0
    Think of it like
    x !== z || x !== q -> true || true -> false
    x !== z && x !== q -> true && true -> true
     
  4. Tweak155

    Tweak155 Lifer

    Joined:
    Sep 23, 2003
    Messages:
    10,591
    Likes Received:
    48
    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.
     
  5. Ken g6

    Ken g6 Programming Moderator, Elite Member
    Moderator

    Joined:
    Dec 11, 1999
    Messages:
    12,651
    Likes Received:
    295
    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. ;)
     
  6. uclabachelor

    uclabachelor Senior member

    Joined:
    Nov 9, 2009
    Messages:
    448
    Likes Received:
    0
    Draw a Karnaugh map and the answer will be obvious.
     
  7. LumbergTech

    LumbergTech Diamond Member

    Joined:
    Sep 15, 2005
    Messages:
    3,624
    Likes Received:
    1
    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