Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Statement for specific truth table

I have three variables, variation, attribute and active for which I need to create an if statement for the following truth table:

variation attribute active Result
[object Object] ‘color’ true TRUE
undefined undefined undefined TRUE
[object Object] ‘color’ false FALSE

But I can’t think of a smart and compact statement for the case, unless I write all desired true scenarios in an or separated long statement…

Can someone please help me with this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is a quick fiddle for you.

TIA.

>Solution :

You can negate all the values to booleans and check if they are all equal:

!variation == !attribute && !attribute == !active

Values in JavaScript can be either truthy or falsy.

  • [object Object], ‘color’, true => truthy
  • undefined, false => falsy

So when you put the logical NOT operator (!) before a value, you effectively negate it, meaning all truthy values become the boolean false and all falsy values are cast to the boolean true.


Your truth table seems to return true when all of the variables are truthy or when all of the variables are falsy, and returns false when they are mixed.

So it essentially checks that all the values, when casted to booleans, are equal. So the condition above casts the values to opposite booleans and checks that they are all the same.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading