There are a number of type conditions

if(a == 1 && b == 2 || a == 2 && b == 1) { c = 4; } if(a == 1 && b == 3 || a == 3 && b == 1) { c = 7; } 

Since the conditions are "symmetrical", they can be simplified if you register in the form of a hash:

 {"1-2":4, "1-3":7, ...} 

But how to use such a hash instead of conditions if there are a and b at the input?

Give at least one simple example to understand the essence

thank

    1 answer 1

    First you need to accurately determine the larger and smaller value, then generate a key and access the hash with a key

     var hash = {"1-2": 4, "1-3": 7}; if (b < a) { var tmp = a; a = b; b = tmp; } var key = a + "-" + b; c = hash[key]