1

## 簡化IF裡面的條件?

``````	let top_val = split_Top.length;
let bottom_val = split_Bottom.length;
if (
(top_val == 0 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 1) ||
(top_val == 1 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 0) ||
(top_val == 1 && bottom_val == 1) ||
(top_val == 4 && bottom_val == 4)
) {
console.log('f0')
}
if (
(top_val == 0 && bottom_val == 2) ||
(top_val == 2 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 0) ||
(top_val == 1 && bottom_val == 2) ||
(top_val == 2 && bottom_val == 1) ||
(top_val == 1 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 1)
) {
console.log('f1')
}
if (
(top_val == 4 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 4) ||
(top_val == 4 && bottom_val == 1) ||
(top_val == 1 && bottom_val == 4) ||
(top_val == 4 && bottom_val == 2) ||
(top_val == 2 && bottom_val == 4) ||
(top_val == 4 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 4)
) {
console.log('f2')
}
if (
(top_val == 2 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 2) ||
(top_val == 3 && bottom_val == 3) ||
(top_val == 2 && bottom_val == 2)
) {
console.log('f3')
}
``````

Han iT邦新手 4 級 ‧ 2020-06-15 17:47:37 檢舉
ㄜ 阿然後咧 是標題那樣 希望可以簡化判斷嗎?

(1,1)呢？
(後來有看到了)
player iT邦大師 1 級 ‧ 2020-06-15 23:49:24 檢舉

### 4 個回答

1

iT邦大神 1 級 ‧ 2020-06-15 18:06:58

``````var dict =
{
"01":"f0", "10":"f0", "00":"f0", "44":"f0",
"02":"f1", "20":"f1", "03":"f1", "30":"f1", "12":"f1", "21":"f1", "13":"f1", "31":"f1",
"40":"f2", "04":"f2", "41":"f2", "14":"f2", "42":"f2", "24":"f2", "43":"f2", "34":"f2",
"23":"f3", "32":"f3", "33":"f3", "22":"f3"
};

for (i=0; i<5; i++) {
for (j=0; j<5; j++) {
top_val = i.toString();
bottom_val = j.toString();
console.log(i,j,dict[top_val+bottom_val]);
}
}
``````

``````function A(x, y) {
if (((x | y) & 4) & (x ^ y)) {
return 'f2';
}
let t = ((x & 3) >>> 1) + ((y & 3) >>> 1);
return 'f' + (t > 1 ? 3 : t);
}

function B(x, y) {
let top_val = x;
let bottom_val = y;
if (
(top_val == 0 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 1) ||
(top_val == 1 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 0) ||
(top_val == 1 && bottom_val == 1) ||
(top_val == 4 && bottom_val == 4)
) {
return 'f0';
}
if (
(top_val == 0 && bottom_val == 2) ||
(top_val == 2 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 0) ||
(top_val == 1 && bottom_val == 2) ||
(top_val == 2 && bottom_val == 1) ||
(top_val == 1 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 1)
) {
return 'f1';
}
if (
(top_val == 4 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 4) ||
(top_val == 4 && bottom_val == 1) ||
(top_val == 1 && bottom_val == 4) ||
(top_val == 4 && bottom_val == 2) ||
(top_val == 2 && bottom_val == 4) ||
(top_val == 4 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 4)
) {
return 'f2';
}
if (
(top_val == 2 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 2) ||
(top_val == 3 && bottom_val == 3) ||
(top_val == 2 && bottom_val == 2)
) {
return 'f3';
}
}

function C(x, y) {
if (x == y ) {
if (x == 0 || x == 4) return "f0";
if (x == 2 || x == 3) return "f3";
} else {
if (x == 4 || y == 4) {
return "f2";
}
else if (x*y == 6) {
return "f3";
}
else if (x+y == 1) {
return "f0";
}
else {
return "f1";
}
}
}

//測試
for (let x = 0; x <= 4; ++x) {
for (let y = 0; y <= 4; ++y) {
let a = C(x, y);
let b = B(x, y);
console.log(`\${x} \${y} \${a===b?'pass':[a,b].join(',')}`);
}
}
``````

``````function D(x, y)
{
return 'f'+(0xa2d24>>>(x&6|y<<2&24)&3);
}
``````
4

iT邦研究生 4 級 ‧ 2020-06-15 18:33:15

``````function A(x, y)
{
if(((x|y)&4)&(x^y)) {
return 'f2';
}
let t=((x&3)>>>1)+((y&3)>>>1);
return 'f'+(t>1?3:t);
}

function B(x, y)
{
let top_val = x;
let bottom_val = y;
if (
(top_val == 0 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 1) ||
(top_val == 1 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 0) ||
(top_val == 1 && bottom_val == 1) ||
(top_val == 4 && bottom_val == 4)
) {
return 'f0';
}
if (
(top_val == 0 && bottom_val == 2) ||
(top_val == 2 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 0) ||
(top_val == 1 && bottom_val == 2) ||
(top_val == 2 && bottom_val == 1) ||
(top_val == 1 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 1)
) {
return 'f1';
}
if (
(top_val == 4 && bottom_val == 0) ||
(top_val == 0 && bottom_val == 4) ||
(top_val == 4 && bottom_val == 1) ||
(top_val == 1 && bottom_val == 4) ||
(top_val == 4 && bottom_val == 2) ||
(top_val == 2 && bottom_val == 4) ||
(top_val == 4 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 4)
) {
return 'f2';
}
if (
(top_val == 2 && bottom_val == 3) ||
(top_val == 3 && bottom_val == 2) ||
(top_val == 3 && bottom_val == 3) ||
(top_val == 2 && bottom_val == 2)
) {
return 'f3';
}
}

//測試
for(let x=0;x<=4;++x) {
for(let y=0;y<=4;++y) {
let a=A(x,y);
let b=B(x,y);
console.log(`\${x} \${y} \${a===b?'pass':[a,b].join(',')}`);
}
}
``````
0

iT邦新手 3 級 ‧ 2020-06-17 09:03:48
``````let codesLt = [
['00', '01', '10', '11', '44'],
['02', '20', '03', '30', '12', '21', '13', '31'],
['40', '04', '41', '14', '42', '24', '43', '34'],
['23', '32', '33', '22']
];

let top_val = 2;
let bottom_val = 1;

for(let i = 0, compared = [top_val, bottom_val].join('');
i < codesLt.length;
i++) {

if(codesLt[i].some(code => code === compared)) {
console.log(['f', i].join(''));
break;
}
}
``````

ch_lute iT邦新手 5 級 ‧ 2020-06-20 14:11:11 檢舉

2
Samと可樂快跑
iT邦研究生 1 級 ‧ 2020-06-17 10:34:00

``````let arr = [
['f0','f0','f1','f1','f2'],
['f0','f0','f1','f1','f2'],
['f1','f1','f3','f3','f2'],
['f1','f1','f3','f3','f2'],
['f2','f2','f2','f2','f0']
]

console.log(arr[2][0])
``````

(雖然上面幾個回答已經有很簡化的做法了………)
(沒辦法，『討論』的部份沒辦法上code，只好用回答代替討論。)

bottom_val 當y值

``````let top_val = split_Top.length;
let bottom_val = split_Bottom.length;

let arr = [
['f0','f0','f1','f1','f2'],
['f0','f0','f1','f1','f2'],
['f1','f1','f3','f3','f2'],
['f1','f1','f3','f3','f2'],
['f2','f2','f2','f2','f0']
]

console.log(arr[top_val][bottom_val]);
``````