调用return语句时,Javascript不退出for循环

时间:2019-03-09 06:17:36

标签: javascript function for-loop return infinite-loop

即使在到达for语句之后,在函数内部的return循环中进行迭代,循环仍会无限进行。

此时,j大于lister.length。它退出了for循环,并在函数结束时以看似无限的电路跳回到了for循环。

这种行为对我来说没有意义,因为return语句应终止该函数。


这是我的功能:

function permutationLoop(originalArray, listOfPermutations) {

    // generates a permutation(Shuffle),and makes sure it is not already in the list of Perms
    var lister = generatingPerms(originalArray, listOfPermutations);

    //adds the permutation to the list
    listOfPermutations.push(lister);

    var tester = true;

    //This for loop looks through the new permutation to see if it is in-order.
    for (var j = 0; j < lister.length; j++) {

        //This if statement checks to see as we iterate if it is in order
        if (lister[j] > lister[j + 1]) {
            tester = false;
        }

        if (j == (lister.length - 1) && tester == true) {
            //Return the permutation number that found the ordered array.

            return listOfPermutations.length;
            //THIS IS NOT EXITING THE LOOP
        }

        if (j == lister.length - 1 && tester == false) {
            permutationLoop(originalArray, listOfPermutations);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的if语句可能无效 尝试通过fun testStuff(type: Class<out Bag<Int>>)

进行测试