How do I find all possible permutations for a specific number?

A very good usecase for generators:

  function* sum(target, numbers, previous = []) {
    if(target === 0) yield previous;
    if(target <= 0) return;
    for(const n of numbers) 
       yield* sum(target - n, numbers, [...previous, n]);
  }

Browse More Popular Posts

Leave a Comment