0

I'm trying to create an output array that is made up of individual arrays that contain every combo of the input array using recursion. I'm having an issue with trying to figure out how to separate the elements of the array. I think I need to loop through the array somehow and keep track of each element.

function getAllCombos(arr) {
  const output = [];

  function recursive(arr, combos = []) {
    if (arr.length === 0) return output.push(combos);
    combos.push(arr);
    recursive(arr.slice(1), [...combos])
  }
  recursive(arr);
  return output
}

// To check if you've completed the challenge, uncomment this code!
console.log(getAllCombos(['a', 'b'])); // -> [['a','b'], ['a'], ['b'], []]
Kyle
  • 1
  • 3
  • Does this answer your question? [Trying to generate all permutations from array but only getting a small subset](https://stackoverflow.com/questions/56770410/trying-to-generate-all-permutations-from-array-but-only-getting-a-small-subset) – Ivar Jul 27 '21 at 19:57

0 Answers0