简体   繁体   English

代码陷入了一系列的for循环中,我不确定为什么

[英]Code is getting stuck somewhere in a succession of for-loops and I'm not sure why

EDIT - I changed the code to correctly declare variables below but nothing seems to have changed 编辑-我更改了代码以正确地在下面声明变量,但似乎什么都没有改变

I've written code using a for-loop that has to satisfy a number of criteria before executing what's within it. 我已经使用for循环编写了代码,该循环必须先执行许多条件,然后才能执行其中的内容。 The problem is that, somewhere along the way, the code is getting stuck inside one of the loops, causing the computer to crash. 问题在于,在此过程中,某个地方的代码被卡在了其中一个循环中,从而导致计算机崩溃。

I've tried breaking the loop but this doesn't seem to help. 我尝试打破循环,但这似乎无济于事。

function compareKeypoints(varifiedKeypoints) {

  outer_loop: for (i = 0; i < varifiedKeypoints.length; i++) {

    let initialKeypoint = varifiedKeypoints[i];

    for (j = 0; j < varifiedKeypoints.length; j++) {

      let comparisonKeypoint = varifiedKeypoints[j];

      if (initialKeypoint.part != comparisonKeypoint.part) {

        if (Math.abs(comparisonKeypoint.position.x - initialKeypoint.position.x) <= 20
        && Math.abs(comparisonKeypoint.position.y - initialKeypoint.position.y) <= 20) {

          if (keypointsCompatible(initialKeypoint.part, comparisonKeypoint.part)) {

            console.log("Activating part: " + initialKeypoint.part);
            console.log("Activated part: " + comparisonKeypoint.part);

            let keypointPair = {
              point_1: initialKeypoint.part,
              point_2: comparisonKeypoint.part
            }

            console.log("Pushing parts!");
            activeParts.push(keypointPair);

            console.log("breaking loop!");
            break outer_loop;

            console.log("Loop NOT broken!!");

          }
        }
      }
    }
  }

  if (activeParts.length > 0) {
    console.log(activeParts);
  }
}

function keypointsCompatible(keypoint_1, keypoint_2) {

  var outcome = true;

  if (activeParts.length > 0) {

    compatibility_loop: for (i = 0; i < activeParts.length; i++) {

      if (Object.values(activeParts[i]).includes(keypoint_1) && Object.values(activeParts[i]).includes(keypoint_2)) {

        console.log(keypoint_1 + " and " + keypoint_2 + " are not compatible because they already exist as " + activeParts[i].point_1 + " and " + activeParts[i].point_2 + " respectively");

        outcome = false;

        break compatibility_loop;

        console.log("Compatibility NOT broken!!");
      }
    }
  }

  console.log("Compatibility outcome is " + outcome);
  return outcome;
}

The code is suppose to take two values in the same array and compare them. 该代码假定在同一数组中接受两个值并进行比较。 If a number of conditions are met, including if they're a certain distance apart from one another, they will be pushed into a secondary array. 如果满足许多条件,包括彼此之间有一定距离,则将它们推入辅助阵列。 If the values already appear in the secondary array, which the keypointCompatible function is suppose to determine, the loop should either continue looking for other candidates or stop before being called again. 如果值已经出现在辅助数组中(假定keypointCompatible函数可以确定),则循环应继续寻找其他候选对象,或者在再次调用之前停止。 For some reason, however, the code is getting stuck within the keypointCompatible function when it detects that the values have already appeared in the secondary array and the console will repeatedly print "Compatibility is false" until the browser crashes. 但是由于某种原因,当代码检测到值已出现在辅助数组中时,该代码就会卡在keypointCompatible函数中,并且控制台将反复打印“ Compatibility is false”,直到浏览器崩溃为止。

Working Solution 工作方案

Use let or const instead of var or nothing. 使用letconst代替var或不使用。 Your issue may be related to closures and variables reused between loops. 您的问题可能与闭包和循环之间重用的变量有关。 Make sure you use let or const in your loops too. 确保在循环中也使用letconst for (let i=0) . for (let i=0)

When you use let or const , the runtime will create a new instance every time the block or loop iterates. 使用letconst ,每次块或循环迭代时,运行时都会创建一个新实例。 However, using var will reuse the internal allocation. 但是,使用var将重用内部分配。

So what happens with the standard var is the multiple closures or loops each use the same instance of the variable. 因此,使用标准var发生的是多个闭包或循环都使用变量的相同实例。

Unless you want the var behavior, always use let or const . 除非您想要var行为,否则请始终使用letconst


Another Solution 另一种解决方案

Put a newline after the label compatibility_loop compatibility_loop循环标签后放置换行符

Still Another Solution 另一个解决方案

The first function is pushing into activeParts . 第一个功能是推入activeParts The second function is looping activeParts . 第二个功能是循环activeParts This can go on forever, or longer than expected. 这可以永远持续下去,或者比预期的时间更长。 Pushing into the array could possibly make the loop limit never reached. 推入阵列可能会使循环极限永远不会达到。 Put a log on the length of activeParts in the second function to see if it is growing out of control. 在第二个函数中的activeParts长度上放置一个日志,以查看它是否不受控制。

Your code should be OK if varifiedKeypoints.length has reasonable value. 如果varifiedKeypoints.length具有合理的值,则您的代码应该可以。 And all internal variables are declared properly! 并且所有内部变量都正确声明!

You have two loops (this inner can start at j=i+1 to save time and multiple calculations) with few conditions inside. 您有两个循环(内部可以从j=i+1以节省时间和进行多次计算),并且内部条件很少。

function compareKeypoints(varifiedKeypoints) {

  outer_loop: for (let i = 0; i < varifiedKeypoints.length; i++) {

    let initialKeypoint = varifiedKeypoints[i];

    for (let j = i+1; j < varifiedKeypoints.length; j++) {

      let comparisonKeypoint = varifiedKeypoints[j];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不知道为什么我会收到这个 TypeError - Not sure why I'm getting this TypeError 画布代码未更新,我不确定为什么 - Canvas code not updating and I'm not sure why 我陷入了循环任务 - I'm stuck in task with loops 我在Ionic中得到一个未定义的变量,我不确定为什么吗? - I'mg getting an undefined variable in Ionic and I'm not sure why? JavaScript 问题。 我正在学习 for 循环和数组如何与方法一起工作,不知道为什么我在控制台中收到一条 NAN 消息 - JavaScript question. I'm learning how for loops and arrays work alongside a method, not sure why I am getting a NAN message in my console 出现错误:req.validatonErrors 不是 function 但我不知道为什么 - Getting error: req.validatonErrors is not a function but I'm not sure why JS:驱动程序代码返回false,但我不确定为什么? - JS: Driver code returns false and I'm not sure why? 我想制作一个待办事项列表,但我一开始就卡住了,我在这段代码中哪里出错了? - i want to make a todo list but i m stuck at the starting, where am i getting wrong in this code? Javascript数组未定义......我不确定为什么 - Javascript array is undefined… and I'm not sure why 为什么我在文本编辑器中看不到 output,即使我确定代码是正确的? - Why I can't see an output in a text editor, even if I'm sure that the code is right?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM