(n, k)
| 1 | function generateCombinations(n, k) { |
| 2 | let currentCombination = [] |
| 3 | let allCombinations = [] // will be used for storing all combinations |
| 4 | let currentValue = 1 |
| 5 | |
| 6 | function findCombinations() { |
| 7 | if (currentCombination.length === k) { |
| 8 | // Add the array of size k to the allCombinations array |
| 9 | allCombinations.push([...currentCombination]) |
| 10 | return |
| 11 | } |
| 12 | if (currentValue > n) { |
| 13 | // Check for exceeding the range |
| 14 | return |
| 15 | } |
| 16 | currentCombination.push(currentValue++) |
| 17 | findCombinations() |
| 18 | currentCombination.pop() |
| 19 | findCombinations() |
| 20 | currentValue-- |
| 21 | } |
| 22 | |
| 23 | findCombinations() |
| 24 | |
| 25 | return allCombinations |
| 26 | } |
| 27 | |
| 28 | export { generateCombinations } |
no test coverage detected