JSFiddle - React, Tailwind, and code Playground

by Josh Pullen

JavaScript

function choose(array, n) {
  if (n === 0) {
  	return [[]];
  }

	let combinations = [];
	for (let i = 0; i < array.length; i++) {
  	let chosen = array[i];
    let newOptions = array.slice(i + 1);
    combinations.push(
    	...choose(newOptions, n - 1)
      	.map(a => [chosen, ...a])
    );
  }
  return combinations;
}

console.log(choose(["A", "B", "C", "D"], 2))