JavaScript Permutation Issue

Hello Developers,

I need to print 3 items in each array but I am failed to implement it, I have done it and it prints 4 items in each array, and all array should be unique.

function allPermutations (items) {

 
  let results = [];
  function permute(arr, memo) {
    var cur, memo = memo || [];
    for (let i = 0; i < arr.length; i++) {
      cur = arr.splice(i, 1);
      if (arr.length === 0) {
        results.push(memo.concat(cur));
      }
      permute(arr.slice(), memo.concat(cur));
      arr.splice(i, 0, cur[0]);
    }
    return results;
  }
  permute(items);
  return results;
}
 
 var fruits = ["Apple", "Banana", "Coconut","jiga"];
var permutated = allPermutations(fruits);
console.log(permutated);

Where are you checking if array has the wanted number of items?

I am not checking it here.

Why not? :slight_smile: Function must have a way to determine if expected number of items is already in the array, or should it add another.

Could you help me out please ?

We don’t write out solutions for users on the forum. What part of the last hint is confusing you? What can we clarify?

1 Like

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