Problem with No Repeats Please

Problem with No Repeats Please
0.0 0

#1

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');

#2

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


#3

@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');

#4

You’re not passing any arguments to generate.

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


#5

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


#6

@mikeale03 @icartusacrimea

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');