如題
我有兩個數值
根據這兩個數值的不同
進到不一樣的方法
有點類似過濾器的感覺
情況大概是這樣
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')
}
在沒有更好的解答出現之前
可以考慮看看以下的寫法(用 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(',')}`);
}
}
我化簡的是 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(',')}`);
}
}
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;
}
}
最簡單的方法:
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]);