My homework assignment has me check for all possible cycle notations from user inputted numbers. I have the input sent into an array but i'm not sure how to start the loop. How could I edit this loop to not display the same numbers more than once? Sorry if this isn't proper format, first time posting.
// example of user input
var permutation = [ 9,2,3,7,4,1,8,6,5 ] ;
// add zero to match index with numbers
permutation.unshift(0) ;
// loop to check for all possible permutations
for (var i = 1; i < permutation.length -1; i++)
{
var cycle = [];
var currentIndex = i ;
if ( permutation [ currentIndex ] == i )
cycle.push(permutation [currentIndex]);
while ( permutation [ currentIndex ] !== i )
{
cycle.push( permutation [currentIndex]);
currentIndex = permutation [ currentIndex ] ;
}
// display in console
console.log(cycle);
}