简体   繁体   English

Javascript:太多的递归错误

[英]Javascript: too much recursion error

I've been working on this project, and I have this array set up that I populate with the following: 我一直在从事这个项目,并且已经设置了以下数组:

var myArray = {};
myArray[idNumber] = parentIdNumber;

Each member can only have one or no parent, denoted by 0. I have values up to 70 filled out. 每个成员只能有一个或没有一个父代,用0表示。我最多可以填写70个值。

I'm receiving a value input and need to check if every member of the array is in the 'family line' of input . 我收到一个值input ,需要检查数组的每个成员是否在input的“族系”中。

So I iterate through myArray and call isInFamilyLine(currentObject, 37) , where currentObject is in the range 1-70: 因此,我遍历myArray并调用isInFamilyLine(currentObject, 37) ,其中currentObject的范围为1-70:

function isInFamilyLine(idNumber, input) {
    if (idNumber== input) {
        isInLine =  true;
    } else {
        if (myArray[idNumber] == 0) {
            isInLine =   false;
        } else {
            isInLine = isInFamilyLine(myArray[idNumber], input);
        }
    }
    return isInLine;
}

I think the logic is right, but most examples I've seen of too much recursion involved a bug in the code. 我认为逻辑是正确的,但是我看到的大多数递归过多的示例都涉及代码中的错误。

What's also weird is that the too much recursion error is thrown on this line: 还奇怪的是,在这一行上引发了too much recursion错误:

if (myArray[idNumber] == 0) {

Any ideas? 有任何想法吗?

Given your code, what probably happens is, due to the bad input, nothing gets executed except your next call to isInFamilyLine which is also passed a bad parameter, giving rise to new calls to itself ad nauseum. 给定您的代码,由于错误的输入,可能发生的事情是什么,除了您下次调用isInFamilyLine ,什么也不会执行,该调用也传递了错误的参数,从而引起了对自身广告的新调用。

If myArray is an associative array, you need to make sure the values for the keys you're passing are of the proper type. 如果myArray是关联数组,则需要确保要传递的键的值具有正确的类型。 You also need to check that whatever you're passing into the function turns out the way you want it and that you don't have any out-of-bounds issues with your array. 您还需要检查传递给函数的内容是否符合您的要求,并且数组没有任何越界问题。

I see two possible problems: 我看到两个可能的问题:

Invalid input (thanks IvyLynx ) 无效的input (感谢IvyLynx

isInFamilyLine(x, "peanuts") leads to infinite recursion because x != "peanuts" for any sensible value of x and myArray["peanuts"] != 0 because undefined != 0 . isInFamilyLine(x, "peanuts")导致无限递归,因为对于x != "peanuts"的任何合理值, x x != "peanuts" ,并且myArray["peanuts"] != 0因为undefined != 0 This leads to this line always being called: 这导致该行始终被称为:

isInLine = isInFamilyLine(myArray[idNumber], input);

So we have an infinite loop. 所以我们有一个无限循环。 Solution is to check if myArray[input] === undefined , then throwing an error and asking the user for a new value. 解决方案是检查myArray[input] === undefined ,然后引发错误并询问用户新值。 This also ensure the input is within the expected range. 这也可以确保输入在预期范围内。

Note that because Javascript is weakly typed and you are using == instead of === , "2" == 2 , so receiving input numbers as string should not be the source of the problem. 请注意,因为Javascript的类型很弱,并且您使用==而不是=== ,所以"2" == 2 ,因此接收输入数字作为字符串不应成为问题的根源。

Circular Structure 圆形结构

If myArray[x] == [x] for any x this code will also go into infinite recursion. 如果myArray[x] == [x]对于任何x此代码也将进入无限递归。 You assume the values are acyclic but this may not be true. 您假定这些值是非循环的,但这可能不是正确的。 The same problem happen with cycles of any size, for example 例如,任何大小的循环都会发生相同的问题

myArray[1] = 2
myArray[2] = 3
myArray[3] = 1

If you only have 70 values a simple solution is to remember which members you have visited and to throw an error if you ever reach the same member twice. 如果只有70个值,一个简单的解决方案是记住您访问过哪些成员,并且两次访问同一成员都会引发错误。 Or you could run a check before accepting any inputs, see How do I check if a directed graph is acyclic . 或者,您可以在接受任何输入之前运行检查,请参阅如何检查有向图是否为非循环的


PS: I'm not sure but I think this algorithm is equivalent to verifying if every node in a tree is reachable or can reach a given input node. PS:我不确定,但我认为此算法等效于验证树中的每个节点是否都可以到达或可以到达给定的输入节点。 Modelling your data as an actual tree and using standard graph algorithms may help. 将数据建模为实际树并使用标准图形算法可能会有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM