Guest User

Untitled

a guest
Oct 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. function permutation(collection) {
  2. let current;
  3. let result = [];
  4. const currentArray = [];
  5. const newResultArray = [];
  6.  
  7. if (collection.length) {
  8. current = collection.shift();
  9. result = permutation(collection);
  10.  
  11. currentArray.push(current);
  12.  
  13. result.map((list) => {
  14. newResultArray.push(list.slice(0));
  15. list.push(current);
  16. });
  17.  
  18. result.push(currentArray);
  19. result = result.concat(newResultArray);
  20. }
  21.  
  22. return result;
  23. }
  24.  
  25. console.log(permutation(['a','b','c','d'])
  26. .join('\n'));
Add Comment
Please, Sign In to add comment