iT邦幫忙

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 檢舉
ㄜ 阿然後咧 是標題那樣 希望可以簡化判斷嗎?
內文有點不清楚你想問什麼
基本上就是減少重複的程式碼
淺水員 iT邦研究生 4 級 ‧ 2020-06-15 18:01:20 檢舉
(1,1)呢?
(後來有看到了)
player iT邦大師 1 級 ‧ 2020-06-15 23:49:24 檢舉
邏輯化簡
記得是真值表或卡諾圖的天下
記得是【計算機概論】的入門課程吧
1
海綿寶寶
iT邦大神 1 級 ‧ 2020-06-15 18:06:58

在沒有更好的解答出現之前
可以考慮看看以下的寫法(用 dictionary 的寫法)

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 C)

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(',')}`);
  }
}

感謝!

淺水員 iT邦研究生 4 級 ‧ 2020-06-16 15:41:16 檢舉

我也來融合一下查表的概念

function D(x, y)
{
	return 'f'+(0xa2d24>>>(x&6|y<<2&24)&3);
}
4
淺水員
iT邦研究生 4 級 ‧ 2020-06-15 18:33:15

我化簡的是 function A
原版的是 function B
以下含測試驗證兩個函式結果相同

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;
    }
}

雖說問題沒有貴賤之分
但是有大師蒞臨指導
真是蓬蓽生輝
/images/emoticon/emoticon32.gif

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。
只要改改陣列內容就好了。
(雖然上面幾個回答已經有很簡化的做法了………)
(沒辦法,『討論』的部份沒辦法上code,只好用回答代替討論。)

可惡!!被你先回答了。
我只好放一下馬後炮了。

基本原則只要當成xy方格應用方式就好了。
如叔叔這樣的寫法。

將 top_val 當x值
bottom_val 當y值

剩下來的就是將格子填進去就好。
簡單又明白的處理方式。

這個方法,我也很常用。特別在機械設備上。

幫 Samと可樂快跑 加多一點點!

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]);

我要發表回答

立即登入回答