I apologize in advance for a very stupid question! I'm just a novice coder can not write one condition here is such a code

if (c) { if (!a && b) { console.log('a1'); } else { console.log('a2'); } else { if (!(!a && b)) { console.log('a1'); } else { console.log('a2'); } } 

    3 answers 3

     if (!c ^ (!a && b)) { console.log('a1'); } else { console.log('a2'); } 

    The condition is the following: с and (not a and b) or not c and not(not a and b) . Replacing not c by x , (not a and b) by y we get not x and y or x and not y , which is equivalent to x xor y . As a result, we have not c xor (not a and b)

    In fact, there is no logical XOR in Javascript, but the bit one also works well, provided that both operands are boolean values

    • the principle is correct, but check the first branch, there c=true - Mi Ke Bu
    • thanks, everything works - baneling
    • @MiKeBu Everything is correct - a xor b == not a and b or a and not b t a a xor b == not a and b or a and not b
    • @tutankhamun, yes, you're right, I was mistaken, sorry) - Mi Ke Bu
    • @MiKeBu Do not apologize :) Everyone can make a mistake - tutankhamun Sept

    Let's turn everything into boolean:

     a = !!a, b = !!b, c = !!c; 

    Now the condition turns into

     console.log(c === (!a && b) ? 'a1' : 'a2'); 

    If you want to abandon the additional castes, then you can still pohomichit:

     console.log(!!c === !!(!a && b) ? 'a1' : 'a2'); console.log(!c === !(!a && b) ? 'a1' : 'a2'); console.log(!c === (!!a || !b)) ? 'a1' : 'a2'); 

    I hope I never made a mistake.

       if (!a && b) { console.log('a1'); } console.log('a2'); 
      • 2
        it will not be the same thing that is written from the top - baneling