I'm trying to make a function that makes an array of all the possible permutations without repetition of the chars in a given array. The problem is that when the recursive call of recur() is made, the rest of the for loop is not executed and as a result the var total consists of 1 string.
function recur(arr,wordArr) {
if(arr.length == 0) {
total.push(wordArr);
} else {
for(var i = 0;i < arr.length;i++) {
var temp = arr;
var newwordArr = wordArr;
newwordArr += temp.splice(i, 1);
recur(temp,newwordArr);
}
}
}
var arr = "abc".split("");
var total = [];
recur(arr,'');
console.log(total);
Calling recur([a,b],'') should make total = ['ab','ba']
There is something I'm not seeing or what I'm trying to do is simply not allowed. Couldn't find the answer to this even tought I did a lot of searching.