Во время итерации цикла 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);
}
}
}