1

我有一个循环,我需要以某种模式返回某些数字,因为我可以使用count我可以使用的数字以及数字44-1(3). 我需要返回 5,12,19,26,33 等为真,其他为假。

function saturn(count,perline){
  var line_one = perline; // 5
  var line_two = perline-1; // 4
  var line_both = line_one + line_two; // 7
  var x = (perline+1)+(line_both*(Math.floor(count/(perline+1))-1));
  if(x<0) x = 0;
  var capture;
  if(perline == 1){ //if perline = 1 don't indent any
    capture = false;
  }else if(x == count){
    capture = true;
  }
  console.log("("+perline+"+1)+("+line_both+"*(Math.floor("+count+"/("+perline+"+1))-1)) ="+x);  
  console.log(count+" "+capture);
  return capture;
}

控制台输出。

script.js:54(4+1)+(7*(Math.floor(1/(4+1))-1)) =0
script.js:561 undefined
script.js:54(4+1)+(7*(Math.floor(2/(4+1))-1)) =0
script.js:562 undefined
script.js:54(4+1)+(7*(Math.floor(3/(4+1))-1)) =0
script.js:563 undefined
script.js:54(4+1)+(7*(Math.floor(4/(4+1))-1)) =0
script.js:564 undefined
script.js:54(4+1)+(7*(Math.floor(5/(4+1))-1)) =5
script.js:565 true
script.js:54(4+1)+(7*(Math.floor(6/(4+1))-1)) =5
script.js:566 undefined
script.js:54(4+1)+(7*(Math.floor(7/(4+1))-1)) =5
script.js:567 undefined
script.js:54(4+1)+(7*(Math.floor(8/(4+1))-1)) =5
script.js:568 undefined
script.js:54(4+1)+(7*(Math.floor(9/(4+1))-1)) =5
script.js:569 undefined
script.js:54(4+1)+(7*(Math.floor(10/(4+1))-1)) =12
script.js:5610 undefined
script.js:54(4+1)+(7*(Math.floor(11/(4+1))-1)) =12
script.js:5611 undefined
script.js:54(4+1)+(7*(Math.floor(12/(4+1))-1)) =12
script.js:5612 true
script.js:54(4+1)+(7*(Math.floor(13/(4+1))-1)) =12
script.js:5613 undefined
script.js:54(4+1)+(7*(Math.floor(14/(4+1))-1)) =12
script.js:5614 undefined
script.js:54(4+1)+(7*(Math.floor(15/(4+1))-1)) =19
script.js:5615 undefined
script.js:54(4+1)+(7*(Math.floor(16/(4+1))-1)) =19
script.js:5616 undefined
script.js:54(4+1)+(7*(Math.floor(17/(4+1))-1)) =19
script.js:5617 undefined
script.js:54(4+1)+(7*(Math.floor(18/(4+1))-1)) =19
script.js:5618 undefined
script.js:54(4+1)+(7*(Math.floor(19/(4+1))-1)) =19
script.js:5619 true
script.js:54(4+1)+(7*(Math.floor(20/(4+1))-1)) =26
script.js:5620 undefined
script.js:54(4+1)+(7*(Math.floor(21/(4+1))-1)) =26
script.js:5621 undefined
script.js:54(4+1)+(7*(Math.floor(22/(4+1))-1)) =26
script.js:5622 undefined
script.js:54(4+1)+(7*(Math.floor(23/(4+1))-1)) =26
script.js:5623 undefined
script.js:54(4+1)+(7*(Math.floor(24/(4+1))-1)) =26
script.js:5624 undefined
script.js:54(4+1)+(7*(Math.floor(25/(4+1))-1)) =33
script.js:5625 undefined
script.js:54(4+1)+(7*(Math.floor(26/(4+1))-1)) =33
script.js:5626 undefined
script.js:54(4+1)+(7*(Math.floor(27/(4+1))-1)) =33
script.js:5627 undefined
script.js:54(4+1)+(7*(Math.floor(28/(4+1))-1)) =33
script.js:5628 undefined
4

2 回答 2

2

这就是你想要做的?

return (x - 5) % 7 == 0

对于 5、12、19、26 和 33,它返回 true。

编辑:您已编辑,但我仍然无法理解您的内容,但这无济于事,抱歉

于 2012-05-01T16:32:02.260 回答
0

您需要使用 % 运算符。这个小小提琴演示通过 100 个数字,似乎返回了你想要的那个。基本上每 7 次迭代 % 7 将等于 1,因此只需添加 4 即可获得您所追求的数字。

http://jsfiddle.net/NQr5j/

var iteration = 100,
    store = [];

do {
    if (iteration % 7 == 1) store.push(iteration + 4);
} while (iteration--);

console.log(store);
于 2012-05-01T16:35:59.350 回答