Advertisement
stanevplamen

All combinations

Sep 15th, 2022
842
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.43 KB | Software | 1 0
  1.  
  2. // find all combinations
  3.  
  4. function combinations(str) {
  5.     var fn = function(active, rest, a) {
  6.         if (!active && !rest)
  7.             return;
  8.         if (!rest) {
  9.             a.push(active);
  10.         } else {
  11.             fn(active + rest[0], rest.slice(1), a);
  12.             fn(active, rest.slice(1), a);
  13.         }
  14.         return a;
  15.     }
  16.     return fn("", str, []);
  17. }
  18.  
  19. var test = combinations("abcde");
  20.  
  21. console.log(test);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement