Introduction
When multiple conditions are judged at the same time, and each condition can affect the result, if the if-else is directly judged, it is particularly easy to be confused and make mistakes in interpretation.
We can regard each bit as a bool value according to the characteristics of binary data.
If you want to judge whether it is true in the combination of four conditions, you can use each condition as a bit in binary, and number it from right to left.
The four conditions are a, b, c, d, corresponding to the last 4 digits from right to left, and then save the corresponding values in variables, such as:
a=true, b=false, c=true, d=true, the obtained binary is: result=1011
When it is necessary to judge that a, c, and d are all true, it can be simplified as:
if(result & 11 == 11) //todo
//or 转为10进制
if(parseInt("result",2) == 11) //todo
In the case of n combined conditions, and so on.
Code
Realization of displacement operation
function conditionValue1(datas: (string | boolean | number)[]): number {
let n = 0;
let strs: string[] = [];
datas.reverse().forEach((value, index) => {
const yes = value && value == "1";
strs.push(yes ? "1" : "0");
n += Math.pow(2, index) >> (yes ? 0 : index + 1);
});
console.log(strs);
return n;
}
//测试
console.log(conditionValue1(["1", "0", 1, 0, true, false]));
//结果
101010;
42;
String concatenation implementation
function conditionValue2(datas: (string | boolean | number)[]): number {
let n = "";
datas.forEach((value, index) => {
const yes = value && value == "1";
n += yes ? "1" : "0";
});
console.log(n);
return parseInt(n, 2);
}
//测试
console.log(conditionValue2(["1", "0", 1, 0, true, false]));
//结果
101010;
42;
example
Suppose we have the following judgment table:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
---|---|---|---|---|---|---|---|---|
break in advance | N | N | N | N | Y | Y | Y | Y |
Last cycle reward > 0 | N | Y | N | Y | N | Y | N | Y |
Rewards for this cycle > 0 | N | N | Y | Y | N | N | Y | Y |
result | Receive (gray) | Receive (bright) | Receive (gray) | Receive (bright) | break (ash) | Receive (bright) | break (bright) | Receive (bright) |
action | none | Receive award | none | Receive award | none | Receive award | smash reward | Receive award |
It can be seen from the table:
- Condition a=whether to break in advance
- Condition b = last cycle reward > 0
- Condition c=This cycle reward>0
In the case of a clear judgment table, we can easily come up with the following code.
private initBtnType() {
if (this._confData) {
const a = this._confData.early_break_status;
const b = this._confData.previous_rebate_amount > 0;
const c = this._confData.current_rebate_amount > 0;
const result = this.conditionValue([a, b, c]);
switch (result + 1) {
case 2:
case 4:
case 6:
case 8:
//领取(亮)
break;
case 7:
//打碎(亮)
break;
case 5:
//打碎(灰)
break;
default:
//领取(灰)
break;
}
}
}
Summarize
- This code method is not readable, and it is not friendly to those who will take over the work later.
- Therefore, a logical and clear judgment table is required before the design.
- The more judgment conditions there are, the more convenient this code is, and the two conditions are not needed.