Javascript 循环返回布尔
我有一个循环,我需要返回一个模式中的某些数字作为true,我有Javascript 循环返回布尔,javascript,loops,Javascript,Loops,我有一个循环,我需要返回一个模式中的某些数字作为true,我有计数,以及编号4和4-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*(Ma
计数
,以及编号4
和4-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;
}
这就是你想要做的
return (x - 5) % 7 == 0
对于5、12、19、26和33,返回true
编辑:您已编辑,我仍然无法理解您的内容,但这不会有帮助,很抱歉,您需要使用%运算符。这个小提琴演示通过100个数字,似乎返回了一个你要找的。基本上,每7次迭代%7将等于1,因此只需添加4即可获得所需的数字
你试过什么?什么特别不起作用?算法
(4+1)+((4-1)+4)*(数学地板(计数/(4+1))-1))
我快发疯了=[它工作的第一对数字,但后来得到抵消。考虑更新你的问题与你正在尝试写的算法的细节,为什么你不能让它工作。预期的真/假输入的例子也将是有益的。我更新它!请看一看!
return (x - 5) % 7 == 0
var iteration = 100,
store = [];
do {
if (iteration % 7 == 1) store.push(iteration + 4);
} while (iteration--);
console.log(store);