Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 协助查找包含模运算符的if语句中的模式_Javascript_Function_Modulus - Fatal编程技术网

Javascript 协助查找包含模运算符的if语句中的模式

Javascript 协助查找包含模运算符的if语句中的模式,javascript,function,modulus,Javascript,Function,Modulus,我在一个个人项目中工作,我发现自己重复了很多代码: // this code is in a function with params k & dir // where i equals a number from 0-2 inclusive // j = (i + 1) % 2 // k = (j + 1) % 2 // The objective is to change a.x, a.y, b.x, & b.y 1. if(k === 0 && d

我在一个个人项目中工作,我发现自己重复了很多代码:

// this code is in a function with params k & dir

// where i equals a number from 0-2 inclusive
// j = (i + 1) % 2
// k = (j + 1) % 2
// The objective is to change a.x, a.y, b.x, & b.y



1.  if(k === 0 && dir === -1) { a.x = dir *  array[k].x; a.y = dir * array[k].y; b.x = dir * -array[i].x; b.y = dir * -array[i].y; }
2.  if(k === 0 && dir ===  1) { a.x = dir *  array[i].x; a.y = dir * array[i].y; b.x = dir * -array[k].x; b.y = dir * -array[k].y; }
3.
4.  if(k === 1 && dir === -1) { a.x = dir * -array[i].x; a.y = dir * array[i].y; b.x = dir * -array[i].x; b.y = dir * -array[i].y; }
5.  if(k === 1 && dir ===  1) { a.x = dir * -array[i].x; a.y = dir * array[i].y; b.x = dir * -array[i].x; b.y = dir * -array[i].y; }
6.  
7.  if(k === 2 && dir === -1) { a.x = dir * -array[j].x; a.y = dir * array[j].y; b.x = dir *  array[i].x; b.y = dir * -array[i].y; }
8.  if(k === 2 && dir ===  1) { a.x = dir * -array[i].x; a.y = dir * array[i].y; b.x = dir *  array[j].x; b.y = dir * -array[j].y; }
两天来,我一直在努力减少代码,但没有任何运气,我坦率地相信,一定有一种方法可以避免使用所有这些if语句

我只是想在正确的方向上引起轰动,尽管一个完整的解决方案是非常受欢迎的

到目前为止,我尝试过再次使用模运算符,但问题出现在第7行和第8行,因为数组索引根据对象是
a
还是
b
而不同。所以我相信至少有两个if语句,但我完全迷路了

再次强调,这比任何事情都更成为一个难题,所以我希望这至少对编码专家来说是一件有趣的事情:)

编辑 代码已经运行了,只是简单地重复了一遍,我希望找到一个更简洁的解决方案。所有值都是数字,
a
b
都是包含
{x:undefined,y:undefined}
的对象

为清晰起见,分为两个对象:

    // for a
    if(k === 0 && dir === -1) { a.x = dir *  array[k].x; a.y = dir *  array[k].y; } // (0, -1) => (0)
    if(k === 0 && dir ===  1) { a.x = dir *  array[i].x; a.y = dir *  array[i].y; } // (0,  1) => (1)

    if(k === 1 && dir === -1) { a.x = dir * -array[i].x; a.y = dir *  array[i].y; } // (1, -1) => (2)
    if(k === 1 && dir ===  1) { a.x = dir * -array[i].x; a.y = dir *  array[i].y; } // (1,  1) => (2)

    if(k === 2 && dir === -1) { a.x = dir * -array[j].x; a.y = dir *  array[j].y; } // (2, -1) => (1)
    if(k === 2 && dir ===  1) { a.x = dir * -array[i].x; a.y = dir *  array[i].y; } // (2,  1) => (0)


    // for b
    if(k === 0 && dir === -1) { b.x = dir * -array[i].x; b.y = dir * -array[i].y; } // (0, -1) => (1)
    if(k === 0 && dir ===  1) { b.x = dir * -array[k].x; b.y = dir * -array[k].y; } // (0,  1) => (0)

    if(k === 1 && dir === -1) { b.x = dir * -array[i].x; b.y = dir * -array[i].y; } // (1, -1) => (2)
    if(k === 1 && dir ===  1) { b.x = dir * -array[i].x; b.y = dir * -array[i].y; } // (1,  1) => (2)

    if(k === 2 && dir === -1) { b.x = dir *  array[i].x; b.y = dir * -array[i].y; } // (2, -1) => (0)
    if(k === 2 && dir ===  1) { b.x = dir *  array[j].x; b.y = dir * -array[j].y; } // (2,  1) => (1)
每行末尾的注释显示input=>output,其中
(k,dir)=>output
,而输出值是给定给
数组的索引

array
是函数范围之外的数组

编辑2
for(设i=0;i<3;i++){
//列表中的数组包含二维平面中的坐标,并遵循三角形网格。每个值如下所示[0,2,-1]
//j=给定i的基础3中的下一个模量值
//k=给定j的基础3中的下一个模量值
设j=(i+1)%3;
设k=(i+2)%3;
列表[i]=[…旧列表].sort((a,b)=>{//排序列表
//这提供了3个按xy、yz和zx排序的排序列表
如果(a[i]==b[i]){
if(a[j]==b[j]&&Math.abs(a[k]-b[k])){
我的_函数(a,b,i,j,k);
}
返回a[j]-b[j];
}
返回a[i]-b[i];
});
}
my_函数(a,b,i,j,k){
设modules=universe.modules;
设dir=b[k]-a[k];
a={x:undefined,y:undefined};
b={x:undefined,y:undefined};
如果(k==0&&dir==1){a.x=dir*module.constraints[k].x;a.y=dir*module.constraints[k].y;}//(0,-1)=>(0)
如果(k==0&&dir==1){a.x=dir*module.constraints[i].x;a.y=dir*module.constraints[i].y;}//(0,1)=>(1)
如果(k==1&&dir==1){a.x=dir*-module.constraints[i].x;a.y=dir*module.constraints[i].y;}//(1,-1)=>(2)
如果(k==1&&dir==1){a.x=dir*-module.constraints[i].x;a.y=dir*module.constraints[i].y;}//(1,1)=>(2)
如果(k==2&&dir==1){a.x=dir*-module.constraints[j].x;a.y=dir*module.constraints[j].y;}//(2,-1)=>(1)
如果(k==2&&dir==1){a.x=dir*-module.constraints[i].x;a.y=dir*module.constraints[i].y;}//(2,1)=>(0)
如果(k==0&&dir==1){b.x=dir*-module.constraints[i].x;b.y=dir*-module.constraints[i].y;}/(0,-1)=>(1)
如果(k==0&&dir==1){b.x=dir*-module.constraints[k].x;b.y=dir*-module.constraints[k].y;}/(0,1)=>(0)
如果(k==1&&dir==1){b.x=dir*-module.constraints[i].x;b.y=dir*-module.constraints[i].y;}/(1,-1)=>(2)
如果(k==1&&dir==1){b.x=dir*-module.constraints[i].x;b.y=dir*-module.constraints[i].y;}/(1,1)=>(2)
如果(k==2&&dir==1){b.x=dir*module.constraints[i].x;b.y=dir*-module.constraints[i].y;}//(2,-1)=>(0)
如果(k==2&&dir==1){b.x=dir*module.constraints[j].x;b.y=dir*-module.constraints[j].y;}//(2,1)=>(1)
//使用Matter.js(physics library)来决定在两个实体之间添加约束的位置
}

此代码可能有问题。自从我开始写这篇文章以来,这个问题已经改变了,我可能没有跟上。但我认为这至少显示了一种避免重复的技巧:

const rules=[[0,-1,'k','k','i','i'],[0,1,'i','i','k','k'],/*…*/]
常量foo=(i,j,k,dir,array)=>{
常数idxs={i,j,k}
常数a={},b={}
常量[,ax,ay,bx,by]=规则。查找(([rk,rd,…)=>rk==k&&rd==dir)
a、 x=dir*数组[idxs[ax]].x
a、 y=dir*数组[idxs[ay]].y
b、 x=dir*-array[idxs[bx]].x
b、 y=dir*-array[idxs[by]].y
返回{a,b}
}

log(foo(0,1,0,-1,[{x:1,y:2}])
这段代码可能有问题。自从我开始写这篇文章以来,这个问题已经改变了,我可能没有跟上。但我认为这至少显示了一种避免重复的技巧:

const rules=[[0,-1,'k','k','i','i'],[0,1,'i','i','k','k'],/*…*/]
常量foo=(i,j,k,dir,array)=>{
常数idxs={i,j,k}
常数a={},b={}
常量[,ax,ay,bx,by]=规则。查找(([rk,rd,…)=>rk==k&&rd==dir)
a、 x=dir*数组[idxs[ax]].x
a、 y=dir*数组[idxs[ay]].y
b、 x=dir*-array[idxs[bx]].x
b、 y=dir*-array[idxs[by]].y
返回{a,b}
}

log(foo(0,1,0,-1,[{x:1,y:2}]))
您实际上想做什么?什么是a和b?什么是我,j,k?什么是dir?什么是示例输入和预期输出?参数
k&dir
是书面形式还是
i&dir
,基于逻辑,这似乎更有意义?@MattWay我现在包含了更多信息,我相信这超出了问题的范围。Edit有一个错误,现在它被修复了。@ScottSauyet,不,
k&dir
是参数,我不使用
I
的原因是
    for(let i = 0; i < 3; i++) {
        // list has arrays inside containing coordinates in a 2d plane, following a triangular grid. Each value looks like this [0, 2, -1]
        // j = next modulus value in base 3 given i
        // k = next modulus value in base 3 given j
        let j = (i + 1) % 3;
        let k = (i + 2) % 3;

        list[i] = [...old_list].sort((a, b) => { // sort list
            // This provides 3 sorted lists which are sorted by xy, yz, and zx
            if(a[i] === b[i]) {
                if( a[j] === b[j] && Math.abs(a[k] - b[k]) ) {
                    my_function(a, b, i, j, k);
                }
                return a[j] - b[j];
            }
            return a[i] - b[i];
        });
    }

    my_function(a, b, i, j, k) {
        let modules = universe.modules;
        let dir = b[k] - a[k];

        a = {x: undefined, y: undefined};
        b = {x: undefined, y: undefined};

        if(k === 0 && dir === -1) { a.x = dir *  module.constraints[k].x; a.y = dir *  module.constraints[k].y; } // (0, -1) => (0)
        if(k === 0 && dir ===  1) { a.x = dir *  module.constraints[i].x; a.y = dir *  module.constraints[i].y; } // (0,  1) => (1)

        if(k === 1 && dir === -1) { a.x = dir * -module.constraints[i].x; a.y = dir *  module.constraints[i].y; } // (1, -1) => (2)
        if(k === 1 && dir ===  1) { a.x = dir * -module.constraints[i].x; a.y = dir *  module.constraints[i].y; } // (1,  1) => (2)

        if(k === 2 && dir === -1) { a.x = dir * -module.constraints[j].x; a.y = dir *  module.constraints[j].y; } // (2, -1) => (1)
        if(k === 2 && dir ===  1) { a.x = dir * -module.constraints[i].x; a.y = dir *  module.constraints[i].y; } // (2,  1) => (0)



        if(k === 0 && dir === -1) { b.x = dir * -module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (0, -1) => (1)
        if(k === 0 && dir ===  1) { b.x = dir * -module.constraints[k].x; b.y = dir * -module.constraints[k].y; } // (0,  1) => (0)

        if(k === 1 && dir === -1) { b.x = dir * -module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (1, -1) => (2)
        if(k === 1 && dir ===  1) { b.x = dir * -module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (1,  1) => (2)

        if(k === 2 && dir === -1) { b.x = dir *  module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (2, -1) => (0)
        if(k === 2 && dir ===  1) { b.x = dir *  module.constraints[j].x; b.y = dir * -module.constraints[j].y; } // (2,  1) => (1)

        // doing some stuff with Matter.js(physics library) to decide where to add constraints between two bodies
    }