循环返回布尔

时间:2012-05-01 16:22:53

标签: javascript loops

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

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

2 个答案:

答案 0 :(得分:2)

这是你想要做的?

return (x - 5) % 7 == 0

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

编辑:你已经编辑了,我仍然无法理解你是什么,但这不会有帮助,抱歉

答案 1 :(得分: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);