一日,I君问有两个变量i和row如下分布,如何用js找出i跟row的规律.
i row 0 1 1 2 2 3 3 4 4 3 5 2 6 1 7 2 8 3 9 4 10 3 ... ...
遂答:
<script>
function x(i){
let l = [1,2,3,4,3,2];
return l[i%l.length]
}
for(let i=0;i<11;i++)console.log("i=%s,row=%s",i,x(i));
</script>得以下结果:

I君觉得用了数组去罗列方法太笨重了,遂优化:
function x(i){
let maxRow = 5;
let rowLen = (maxRow-1) *2;
let p1 = i % rowLen+1;
return p1<maxRow ? p1 : maxRow - (p1 % maxRow);
}
for(let i=0;i<11;i++)console.log("i=%s,row=%s",i,x(i));得以下结果:

你有什么更刁钻的写法?

