Skip to content
Advertisement

How to print OKAY if required variables are not empty?

JavaScript

As of my example code it is printing okay. But it should be print Okay either both d,e and f are should be empty or both should not to be empty. For example if a,b and c are not empty and d,e and f are empty then it should be print “okay”. And a, b, c are not empty and d is not empty, e and f are empty so in this case it should print “not OKAY”. Or if ab and c are not empty and d, e and f are also not empty then it should be print “OKAY”. How to do. I don’t have an practical idea that’s why i could not given an example.

Truth Table: enter image description here

Advertisement

Answer

So your condition is that all items in a group need to be the same, empty or non-empty, and at least one of the two groups needs to be all non-empty. So you could say that each group has a ternary state: True (all non-empty), False (all empty) or None (mixed emptiness):

JavaScript

Then you would have a comparison like:

JavaScript

Which we can write a little more elegantly as:

JavaScript

We’re taking advantage of the fact that True behaves as 1, False behaves as 0, and None cannot be summed up and results in a TypeError. So if either group results in None, it’ll raise a TypeError and the end result is False. Otherwise, we’ll want at least one group to be truthy, i.e. the sum needs to be at least 1.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement