"No Repeats, Please " freecodecamp task code works, but doesn't go through one test

Tell us what’s happening:
Describe your issue in detail here.
Hi everyone! I recently worked on a “No Repeats Please” task, and got stuck by the output of one test performing on my code.
I came up with my own non-recursive way of finding all possible combinations which seems to work correctly both according to my logic and outputs with small amount of elements. What it does is that it starts at the final element of the initial array (lastIndex) and moves it to the start of this same array by replacing other previous elements and adding new combination to “results” array. I do that by decrementing lastIndex. When lastIndex becomes 1 (which means it will become 0 in the next iteration), my code changes it again to the length-1. By this way, the code finds all permutations for each different first element. The example with [1,2,3,4] first and second elements is:
1,2,3,4

1,2,4,3 // last element 3 swaps with 4, lastIndex decrements

1,4,2,3 // 4 swaps with 2, lastIndex becomes length-1

1,4,3,2 // 3 swaps with 2, lastIndex decrements

1,3,4,2 // 4 swaps with 3, lastIndex becomes length-1 again

1,3,2,4 - 2 swaps with 4, last iteration for the first element ‘1’, the next operation will go again to 1,2,3,4 by swapping 2 and 3, but it doesn’t because if statement at the beginning of loop sees that the number of arrays in results array is divisible by 6, which means that all combinations for this specific element were found

2,1,3,4

2,1,4,3

2,4,1,3

2,4,3,1

2,3,4,1
Finally, when function permutate() adds all possible permutations (factorial of length ) to ‘results’ array, loop breaks and the final array of permutations is returned.
The problem is that for some reason the permAlone(“abfdefa”) does not return 2640 but 3168, though I found that my code that checks repeats in permutations works correctly (I replaced it with other solutions and the result was the same) which means that the problem is somewhere in permutate() function, but I can’t find where. I would very appreciate if someone can help! I also understand that this implementation is probably one of the most ineffective ones , but I’m just interested in making it work , as I came up with it just based on my own reasoning. Thank you for your help!!!
Your code so far


function permAlone(str) {
const array = [...str]
const arrays = permutate(array)
let count = 0
console.log('Arrays', arrays)
for (const arr of arrays) {
  inner: for (const [index, a] of arr.entries()) {
    if (arr[index + 1] === a) {
      count++
      break inner
    }
  }
}
console.log('Final Result', arrays.length - count)
return arrays.length - count
}

function permutate(array) {
const results = []
results.push(array)
const length = array.length
let currentIndex = 1
let lastIndex = length - 1
const totalPerms = factorial(length)
const firstElementPerms = totalPerms / length
for (let result of results) {
  let arr = [...result]
  if (results.length == totalPerms) break
  if (results.length % firstElementPerms == 0) {
    const newarray = [...array]
    newarray[0] = array[currentIndex]
    newarray[currentIndex] = array[0]
    results.push(newarray)
    lastIndex = length - 1
    currentIndex++
    continue
  }
  if (lastIndex - 1 == 0) {
    lastIndex = length - 1
  }
  const temp = arr[lastIndex]
  arr[lastIndex] = arr[lastIndex - 1]
  arr[lastIndex - 1] = temp
  results.push(arr)
  lastIndex--
}
return results
}

function factorial(i) {
if (i > 1) {
  return i * factorial(i - 1)
} else {
  return i
}
}

permAlone('abfdefa')

Challenge: No Repeats Please

Link to the challenge:

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