After many attempts, I finally wrote a function that aims to identify all feasible permutations from an array of numbers. Below is the code snippet.
var perm = [];
var usedchar = [];
function findPossiblePermutations(array) {
for (var index = 0; index < array.length; index++) {
var currentNum = array.splice(index,1)[0];
usedchar.push(currentNum);
if (usedchar.length == 3) {
perm.push(usedchar);
}
findPossiblePermutations(array);
usedchar.pop();
array.splice(index,0,currentNum);
}
}
findPossiblePermutations([1,2,3]);
console.log(perm); //-> [[],[],[],[],[],[]]
My anticipation was to retrieve:
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
I am puzzled as to why my expected output is not being achieved. Could someone shed some light on this issue?