Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I typescrpted it... #12

Open
jonathan-meyer opened this issue Oct 8, 2021 · 1 comment
Open

I typescrpted it... #12

jonathan-meyer opened this issue Oct 8, 2021 · 1 comment

Comments

@jonathan-meyer
Copy link

const combo = <T>(a: T[], min?: number, max?: number): T[][] => {
  min = min || 1;
  max = Math.min(max || a.length, a.length);

  const fn = (n: number, src: T[], got: T[], all: T[][]) => {
    if (n == 0) {
      if (got.length > 0) {
        all[all.length] = got;
      }

      return;
    }

    for (let j = 0; j < src.length; j++) {
      fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
    }

    return;
  };

  const all = [];

  for (let i = min; i <= max; i++) {
    fn(i, a, [], all);
  }

  return all;
};
@jonathan-meyer
Copy link
Author

I also applied the max bug fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant