# Problem with No Repeats Please

I’m confused as to why the permutations array contains nothing in it still. Here is my code, someone please help:

``````var regExp =/(.)\1+/g;
function permAlone(str) {
var newArray = str.split('');
var n = newArray.length;
var permutations = [];

function swap(index1, index2) {
tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}

function generate(n, newArray) {
if (n === 1) {
permutations.push(newArray.join(''));
} else {
for(var i = 0; i<n-1; i++) {
generate(n-1, newArray);
if(n % 2 === 0) {
swap(newArray[i], newArray[n-1]);
} else {
swap(newArray[0], newArray[n-1]);
}
}
generate(n-1, newArray);
}
}
return permutations;
}

permAlone('abc');
``````

To strictly answer your question: your generate function is not initially being called.

@icartusacrimea
I changed some stuff around but now it says maximum call size exceeded

``````var regExp =/(.)\1+/g;
function permAlone(str) {
var newArray = str.split('');
var n = newArray.length;
var permutations = [];
var tmp;

function swap(index1, index2) {
tmp = newArray[index1];
newArray[index1] = newArray[index2];
newArray[index2] = tmp;
}

function generate(n, newArray) {
if (n === 1) {
permutations.push(newArray.join(''));
} else {
for(var i = 0; i<n-1; i++) {
generate(n-1, newArray);
if(n % 2 === 0) {
swap(i, n-1);
} else {
swap(0, n-1);
}
}
generate(n-1, newArray);
}
}
generate();
return permutations;
}

permAlone('abc');
``````

You’re not passing any arguments to generate.

Remember: the solution is number of permutations, not an array of them.

@njanne19 your generate function requires a 2 parameter, n and newArray. You haven’t pass anything in your generate function.

1 Like

Thanks for the help. I’m aware I’m not logging the length of the arrays, I’m just logging all of the possible permutations to see if it works first. My question now is, how come it logs every permutation twice as often.

Here is my code:

``````var regex = /(.)\1+/g;
function permAlone(str) {
var newArray = str.split('');
var n = newArray.length;
var permutations = [];
var tmp;

function swap(index1, index2) {
tmp = newArray[index1];
newArray[index1] = newArray[index2];
newArray[index2] = tmp;
}

function generate(n, newArray) {
if (n === 1) {
permutations.push(newArray.join(''));
} else {
for(var i = 0; i<n-1; i++) {
generate(n-1, newArray);
swap(n % 2 ? 0 : i, n-1);
permutations.push(newArray.join(''));
/*          if(n % 2 === 0) {
swap(i, n-1);
permutations.push(newArray.join(''));
} else {
swap(0, n-1);
permutations.push(newArray.join(''));
} */
}
generate(n-1, newArray);
}
}
generate(n, newArray);
return permutations;
}
permAlone('aab');
``````