UNPKG

dist-javascript-algorithms-and-data-structures

Version:

Algorithms and data-structures implemented on JavaScript

28 lines (23 loc) 880 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = permutateWithRepetitions; /** * @param {*[]} permutationOptions * @param {number} permutationLength * @return {*[]} */ function permutateWithRepetitions(permutationOptions, permutationLength = permutationOptions.length) { if (permutationLength === 1) { return permutationOptions.map(permutationOption => [permutationOption]); } // Init permutations array. const permutations = []; // Go through all options and join it to the smaller permutations. permutationOptions.forEach(currentOption => { const smallerPermutations = permutateWithRepetitions(permutationOptions, permutationLength - 1); smallerPermutations.forEach(smallerPermutation => { permutations.push([currentOption].concat(smallerPermutation)); }); }); return permutations; }