|
1 | 1 | /**
|
2 | 2 | *@param {*[]} comboOptions
|
3 | 3 | *@param {number} comboLength
|
4 |
| - *@param {*[][]} combos |
5 |
| - *@param {*[]} currentCombo |
6 | 4 | *@return {*[]}
|
7 | 5 | */
|
8 |
| -functioncombineRecursively(comboOptions,comboLength,combos=[],currentCombo=[]){ |
9 |
| -if(comboLength===0){ |
10 |
| -combos.push(currentCombo); |
11 |
| - |
12 |
| -returncombos; |
| 6 | +exportdefaultfunctioncombineWithoutRepetitions(comboOptions,comboLength){ |
| 7 | +if(comboLength===1){ |
| 8 | +returncomboOptions.map(comboOption=>[comboOption]); |
13 | 9 | }
|
14 | 10 |
|
| 11 | +constcombos=[]; |
| 12 | + |
| 13 | +// Eliminate characters one by one and concatenate them to |
| 14 | +// combinations of smaller lengths.s |
15 | 15 | for(letletterIndex=0;letterIndex<=(comboOptions.length-comboLength);letterIndex+=1){
|
16 |
| -constletter=comboOptions[letterIndex]; |
17 |
| -constrestCombinationOptions=comboOptions.slice(letterIndex+1); |
| 16 | +constcurrentLetter=comboOptions[letterIndex]; |
18 | 17 |
|
19 |
| -combineRecursively( |
20 |
| -restCombinationOptions, |
| 18 | +constsmallerCombos=combineWithoutRepetitions( |
| 19 | +comboOptions.slice(letterIndex+1), |
21 | 20 | comboLength-1,
|
22 |
| -combos, |
23 |
| -currentCombo.concat([letter]), |
24 | 21 | );
|
| 22 | + |
| 23 | +smallerCombos.forEach((smallerCombo)=>{ |
| 24 | +combos.push([currentLetter].concat(smallerCombo)); |
| 25 | +}); |
25 | 26 | }
|
26 | 27 |
|
27 | 28 | returncombos;
|
28 | 29 | }
|
29 |
| - |
30 |
| -/** |
31 |
| - *@param {*[]} combinationOptions |
32 |
| - *@param {number} combinationLength |
33 |
| - *@return {*[]} |
34 |
| - */ |
35 |
| -exportdefaultfunctioncombineWithoutRepetitions(combinationOptions,combinationLength){ |
36 |
| -returncombineRecursively(combinationOptions,combinationLength); |
37 |
| -} |