diff --git a/Backtracking/AllCombinationsOfSizeK.js b/Backtracking/AllCombinationsOfSizeK.js index 61a1d55c3..547a38c30 100644 --- a/Backtracking/AllCombinationsOfSizeK.js +++ b/Backtracking/AllCombinationsOfSizeK.js @@ -25,19 +25,21 @@ class Combinations { constructor (n, k) { this.n = n this.k = k - this.combinationArray = [] // will be used for storing current combination + this.current = [] // will be used for storing current combination + this.combinations = [] } findCombinations (high = this.n, total = this.k, low = 1) { if (total === 0) { - console.log(this.combinationArray) - return + this.combinations.push([...this.current]) + return this.combinations } for (let i = low; i <= high; i++) { - this.combinationArray.push(i) + this.current.push(i) this.findCombinations(high, total - 1, i + 1) - this.combinationArray.pop() + this.current.pop() } + return this.combinations } } diff --git a/Backtracking/tests/AllCombinationsOfSizeK.test.mjs b/Backtracking/tests/AllCombinationsOfSizeK.test.js similarity index 54% rename from Backtracking/tests/AllCombinationsOfSizeK.test.mjs rename to Backtracking/tests/AllCombinationsOfSizeK.test.js index 0fb3ec1b7..04acf6470 100644 --- a/Backtracking/tests/AllCombinationsOfSizeK.test.mjs +++ b/Backtracking/tests/AllCombinationsOfSizeK.test.js @@ -3,11 +3,11 @@ import { Combinations } from '../AllCombinationsOfSizeK' describe('AllCombinationsOfSizeK', () => { it('should return 3x2 matrix solution for n = 3 and k = 2', () => { const test1 = new Combinations(3, 2) - expect(test1.findCombinations).toEqual([[1, 2], [1, 3], [2, 3]]) + expect(test1.findCombinations()).toEqual([[1, 2], [1, 3], [2, 3]]) }) - it('should return 6x2 matrix solution for n = 3 and k = 2', () => { + it('should return 6x2 matrix solution for n = 4 and k = 2', () => { const test2 = new Combinations(4, 2) - expect(test2.findCombinations).toEqual([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]) + expect(test2.findCombinations()).toEqual([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]) }) })