While iterating through the for
loop inside my function, even after the return
statement is reached, the loop proceeds infinitely.
At this point, j
is greater than lister.length
. It exits the for
loop and at the end of function jumps back to the for
loop in a seemingly endless circuit.
This behaviour doesn't make sense to me as the return
statement should terminate the function.
Here is my function:
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);
}
}
}
may your if statement is not valid
try testing by if(true){ ..code.. }