【问题标题】:Matching iterating values in js when iterating with a higher step count than 1迭代步数大于 1 时匹配 js 中的迭代值
【发布时间】:2021-09-15 03:49:24
【问题描述】:

当我使用 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;
    }
}

【问题讨论】:

  • 你觉得valuetoreach - i 怎么样?这是一个你有信心的数学运算吗?
  • 我需要从 0 开始一直递增,直到我需要的值,但我不能递增 1。我不能使用 valuetoreach-1,因为我需要从 0 动画到valuetoreach 但步数不能是 1 或要达到的数字的倍数。
  • @Andy 感谢您的提示。自从我使用 SO 以来已经有一段时间了。我将提供一个可重现的示例。
  • 现在添加了一个最小可重现示例。
  • 您最近的编辑没有任何意义。如果您高于value_to_reach,为什么要减少计数器?保证会以无限循环结束...

标签: javascript increment


【解决方案1】:

如何检测是否接近53

您可以通过以下方式获得总步数:

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;
            }
        }

【讨论】:

  • 是的,您的两个答案都很有帮助。感谢您提供有关查找步数的提示。我从来没有想过那些事情。
【解决方案2】:
在接近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)

【讨论】:

  • 我明白,但就我而言,如果它较小,我需要增加,如果它更大,我需要减少。尽管有这种情况,我还是需要匹配这个值。我将稍微修改一下示例
  • 不太清楚你的意思是“如果它较小,我需要增加,如果它更大,我需要减少”。请在 OP 中澄清。
  • 你看到我更新的例子了吗?这是一部动画。如果对象太小,我需要以更快的速度继续增加对象的大小。 (即增量速度)。如果对象的大小很大,我需要不断减小它的大小,直到它达到所需的较小大小。在这种情况下,大小为53,速度为5。我不能保持速度为1。
  • 我已经添加了另一个sn-p,不确定哪个是你喜欢的。
  • 嗯,你为什么改变接受的答案?认为第一个 sn-p 和你预期的一样。
猜你喜欢
  • 2011-12-16
  • 1970-01-01
  • 2019-04-06
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 2018-04-06
相关资源
最近更新 更多