UNPKG

dist-javascript-algorithms-and-data-structures

Version:

Algorithms and data-structures implemented on JavaScript

29 lines (24 loc) 802 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = combineWithRepetitions; /** * @param {*[]} comboOptions * @param {number} comboLength * @return {*[]} */ function combineWithRepetitions(comboOptions, comboLength) { if (comboLength === 1) { return comboOptions.map(comboOption => [comboOption]); } // Init combinations array. const combos = []; // Eliminate characters one by one and concatenate them to // combinations of smaller lengths. comboOptions.forEach((currentOption, optionIndex) => { const smallerCombos = combineWithRepetitions(comboOptions.slice(optionIndex), comboLength - 1); smallerCombos.forEach(smallerCombo => { combos.push([currentOption].concat(smallerCombo)); }); }); return combos; }