Please help me on this "No Repeat Please" challenge

In the solution of the challenge by fCC, there is a line in “generate” function:


I understand this function is based on Heap’s algoritm, however if I don’t use join method and only push array “arr” into array “permutation”, the position of “arr” is unchange for each recursion.
Can someone please expain the reason for me?

Here is the solution of the challenge by fCC:

function permAlone(str) {
  // Create a regex to match repeated consecutive characters.
  var regex = /(.)\1+/;

  // Split the string into an array of characters.
  var arr = str.split("");
  var permutations = [];
  var tmp;

  // Return 0 if str contains same character.
  if (str.match(regex) !== null && str.match(regex)[0] === str) return 0;

  // Function to swap variables' content.
  function swap(index1, index2) {
    tmp = arr[index1];
    arr[index1] = arr[index2];
    arr[index2] = tmp;

  // Generate arrays of permutations using the algorithm.
  function generate(int) {
    if (int === 1) {
      // Make sure to join the characters as we create  the permutation arrays
    } else {
      for (var i = 0; i != int; ++i) {
        generate(int - 1);
        swap(int % 2 ? 0 : i, int - 1);


  // Filter the array of repeated permutations.
  var filtered = permutations.filter(function(string) {
    return !string.match(regex);

  // Return how many have no repetitions.
  return filtered.length;

// Test here.
  **Your browser information:**

User Agent is: Mozilla/5.0 (Linux; Android 11; M2007J3SG) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.98 Mobile Safari/537.36

Challenge: No Repeats Please

Link to the challenge:

It’s always the same array, you push references to the array, and when the array changes so do its references.

Have you completed this challenge?

1 Like

Thanks for your answer!

Yes I finished this challenge, however I did not know this is a huge problem until now :frowning:
As I do more research, I realise array is actually a pointer to a list of data stored in a location in memory, not a “data” itself!
So that declaring a variable as array is like bookmarking the “list” with a “name”. That name points to the list. When I push something into an array, that means I change the “list”, not the data of the “name”. the data of the name is still the “list”…

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

That is also explain the swap function, because this modifying the actual data, am I right? :frowning:

Yep, that is changing the array

1 Like

thank you so much!!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.