mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00

* Update AllCombinationsOfSizeK.js * Update AllCombinationsOfSizeK.js * Update AllCombinationsOfSizeK.test.js Changes made it the type of testing. Instead of testing the class now the program will test the function * Update AllCombinationsOfSizeK.js * Update AllCombinationsOfSizeK.js * Update AllCombinationsOfSizeK.js * Update AllCombinationsOfSizeK.test.js * Update AllCombinationsOfSizeK.test.js
29 lines
683 B
JavaScript
29 lines
683 B
JavaScript
function generateCombinations(n, k) {
|
|
let currentCombination = []
|
|
let allCombinations = [] // will be used for storing all combinations
|
|
let currentValue = 1
|
|
|
|
function findCombinations() {
|
|
if (currentCombination.length === k) {
|
|
// Add the array of size k to the allCombinations array
|
|
allCombinations.push([...currentCombination])
|
|
return
|
|
}
|
|
if (currentValue > n) {
|
|
// Check for exceeding the range
|
|
return
|
|
}
|
|
currentCombination.push(currentValue++)
|
|
findCombinations()
|
|
currentCombination.pop()
|
|
findCombinations()
|
|
currentValue--
|
|
}
|
|
|
|
findCombinations()
|
|
|
|
return allCombinations
|
|
}
|
|
|
|
export { generateCombinations }
|