JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//wzrd.in/standalone/expect@latest"></script>
<script src="//wzrd.in/standalone/deep-freeze@latest"></script>
<body>
</body>
JavaScript
const combinations = function*(elements, length) {
for (let i = 0; i < elements.length; i++) {
if (length === 1) {
yield [elements[i]];
} else {
let remaining = combinations(elements.slice(i + 1, elements.length), length - 1);
for (let next of remaining) {
yield [elements[i], ...next];
}
}
};
}
const testCombinations = () => {
expect(Array.from(combinations(['a', 'b', 'c'], 2)))
.toEqual([
['a', 'b'],
['a', 'c'],
['b', 'c']
]);
};
const logdisp = (arg) => {
document.querySelector('body').innerHTML = arg;
console.log(arg)
};
testCombinations();
logdisp('All tests passed');