Checking key pairs in Javascript

I have a business scenario as shown below.

I add lines of possible (key, value) pairs. (Country, climate) as one below.

Image 2 shows the entire scenario "Valid and invalid" ("Key", "Value").


Possible keys are the whole country, India, Australia, America, England

Possible values ​​are all climatic, hot, dry, rainy, cold, wet


Example

if (All Country, All Climate) is selected as the first pair (England, hot) should not be selected as the second pair

If (All country, hot) is selected as the first pair (India, hot), it should not be allowed to be selected as the second pair

If (America, All Climate) is selected as the first pair (America, Hot), do not select as the second pair

where as

If (India, hot) is selected as the first pair (India, wet), then it is allowed to choose as the second pair

If (America, All Climate) is selected as the first pair (England, All Climate), then it is allowed to choose as the second pair

If (America, all climate) is selected as the first pair (India, wet), then it can be selected as the second pair

Image 1

Image1


Image 2

Image2

Question I explained "My colleague" all the permissible and unacceptable possible combinations through Image2.

In javascript, the easiest solution would be to add if else expressions and do this. At the same time, the gap between the one that I explained to solve the business scenario in the document and the code extension is widening.

What would be the best implementation of transferring a matrix in a document to code so that the business in the document and code is closely related.

This question may seem vague, but we all at least once encountered such a problem when things written to solve a problem in a document and ways to solve it using code do not make sense.

+5
source share
1 answer

Use binary arithmetic.

Country Values

India 0001 Australia 0010 America 0100 England 1000 All Country 1111 

Climatic value

 Hot 00001 Dry 00010 Rainy 00100 Cold 01000 Humid 10000 All Climate 11111 

When you select the second item, use the AND operation to decide whether this value will already be selected (ALL or the same value).

Example

 var firstCountry = 0xF var secondCountry = 0x1 if ( (firstCountry & secondCountry) > 0) { console.log("country is already choosen"); } 

Of course, this example is for one attribute only. You must update it for both attributes.

+1
source

All Articles