以高于 1 的步数迭代时匹配 js 中的迭代值
Matching iterating values in js when iterating with a higher step count than 1
当我使用除 1 以外的其他迭代步数时如何匹配值?
请看下面的例子。我以 5 为单位进行交互,因为 5 是速度。
但是,一旦我接近 53,我该如何停止。我如何检测并停止开始超过 53 并达到 53?
var start=0;
var value_to_reach=53;
var increment_speed=5;
while(true) {
if(start>value_to_reach)
{
start-=increment_speed
} else { start+=increment_speed }
if (start==value_to_reach) {
console.log("reached :" + value_to_reach); //Obviously this will never happen with the increment being +5."
}
if (start>54)
{
console.log("Let's break this loop for the sake of stopping this infinite loop. But we couldn't achieve what we want. Not reached " + value_to_reach);
break;
}
}
如果我们接近 target
,请降低台阶
在while (start <= reach)
循环中我们可以:
- 检查下一个迭代器是否在
reach
之上
- 如果是,降低迭代器
- 否则,保持常规步长
- 增量计数器
let start = 0,
steps = 5,
reach = 53,
debug = 0;
while (start <= reach) {
console.log(`Step: ${steps}, current: ${start}`);
// If next iteration will be above reach
if ((start + steps) >= reach) {
// Set steps to the diff
steps = (start + steps) - reach;
}
// Increment
start += steps;
}
这将输出:
Step: 5, current: 0
Step: 5, current: 5
Step: 5, current: 10
Step: 5, current: 15
Step: 5, current: 20
Step: 5, current: 25
Step: 5, current: 30
Step: 5, current: 35
Step: 5, current: 40
Step: 5, current: 45
Step: 5, current: 50
Step: 2, current: 52
Step: 1, current: 53
如果你想超过 reach
,然后回到 target
,你可以使用类似的东西:
let start = 0,
steps = 5,
reach = 53,
debug = 0;
do {
console.log(`Step: ${steps}, current: ${start}`);
// If we're above the target
if (start > reach) {
// Set steps to negative
steps = -1;
}
// Increment
start += steps;
console.log(`Step: ${steps}, current: ${start}`);
} while (start !== reach)
How to detect if it's close to 53 or not
你可以通过这个得到总步数:
var totalRepeation = Math.ceil(value_to_reach / increment_speed);
因此您可以创建一个递增的计数器并检查最后一步。
你可以试试这个:
var start = 0;
var value_to_reach = 53;
var increment_speed = 5;
let totalRepeation = Math.ceil(value_to_reach / increment_speed);
let i = 0;
while (true) {
if (start > value_to_reach) {
start -= increment_speed
} else { start += increment_speed; i++ }
if (i + 1 >= totalRepeation) {
console.log("you are close to 53. the start numbre is: ", start)
break;
}
if (start == value_to_reach) {
console.log("reached :" + value_to_reach); //Obviously this will never happen with the increment being +5."
}
if (start > 54) {
console.log("Let's break this loop for the sake of stopping this infinite loop. But we couldn't achieve what we want. Not reached " + value_to_reach);
break;
}
}
当我使用除 1 以外的其他迭代步数时如何匹配值?
请看下面的例子。我以 5 为单位进行交互,因为 5 是速度。
但是,一旦我接近 53,我该如何停止。我如何检测并停止开始超过 53 并达到 53?
var start=0;
var value_to_reach=53;
var increment_speed=5;
while(true) {
if(start>value_to_reach)
{
start-=increment_speed
} else { start+=increment_speed }
if (start==value_to_reach) {
console.log("reached :" + value_to_reach); //Obviously this will never happen with the increment being +5."
}
if (start>54)
{
console.log("Let's break this loop for the sake of stopping this infinite loop. But we couldn't achieve what we want. Not reached " + value_to_reach);
break;
}
}
如果我们接近 target
,请降低台阶
在while (start <= reach)
循环中我们可以:
- 检查下一个迭代器是否在
reach
之上- 如果是,降低迭代器
- 否则,保持常规步长
- 增量计数器
let start = 0,
steps = 5,
reach = 53,
debug = 0;
while (start <= reach) {
console.log(`Step: ${steps}, current: ${start}`);
// If next iteration will be above reach
if ((start + steps) >= reach) {
// Set steps to the diff
steps = (start + steps) - reach;
}
// Increment
start += steps;
}
这将输出:
Step: 5, current: 0
Step: 5, current: 5
Step: 5, current: 10
Step: 5, current: 15
Step: 5, current: 20
Step: 5, current: 25
Step: 5, current: 30
Step: 5, current: 35
Step: 5, current: 40
Step: 5, current: 45
Step: 5, current: 50
Step: 2, current: 52
Step: 1, current: 53
如果你想超过 reach
,然后回到 target
,你可以使用类似的东西:
let start = 0,
steps = 5,
reach = 53,
debug = 0;
do {
console.log(`Step: ${steps}, current: ${start}`);
// If we're above the target
if (start > reach) {
// Set steps to negative
steps = -1;
}
// Increment
start += steps;
console.log(`Step: ${steps}, current: ${start}`);
} while (start !== reach)
How to detect if it's close to 53 or not
你可以通过这个得到总步数:
var totalRepeation = Math.ceil(value_to_reach / increment_speed);
因此您可以创建一个递增的计数器并检查最后一步。
你可以试试这个:
var start = 0;
var value_to_reach = 53;
var increment_speed = 5;
let totalRepeation = Math.ceil(value_to_reach / increment_speed);
let i = 0;
while (true) {
if (start > value_to_reach) {
start -= increment_speed
} else { start += increment_speed; i++ }
if (i + 1 >= totalRepeation) {
console.log("you are close to 53. the start numbre is: ", start)
break;
}
if (start == value_to_reach) {
console.log("reached :" + value_to_reach); //Obviously this will never happen with the increment being +5."
}
if (start > 54) {
console.log("Let's break this loop for the sake of stopping this infinite loop. But we couldn't achieve what we want. Not reached " + value_to_reach);
break;
}
}