在我的函数内部遍历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);
        }
    }
}

最佳答案

可能您的if语句无效
尝试通过if(true){ ..code.. }进行测试

09-19 16:16