From 250eea2acbc5fab7711ca0022c129cd71c3292f7 Mon Sep 17 00:00:00 2001 From: Hector Remedios Date: Sun, 16 Aug 2020 22:30:29 -0500 Subject: [PATCH] This is my solution to Challenge-Javascript-06 --- src/index.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 2f25fb2..859f234 100644 --- a/src/index.js +++ b/src/index.js @@ -8,8 +8,35 @@ */ const combinationSumRecursive = ( - - } + candidates, + remainingSum, + finalCombinations = [], + currentCombination = [], + startFrom = 0 + ) => { + if (remainingSum === 0) { + finalCombinations.push(currentCombination.slice()); + currentCombination = []; + return finalCombinations; + } + else { + if (remainingSum < 0) + return []; + else { + for (let currentIndex = startFrom; currentIndex < candidates.length; currentIndex++) { + const currentNumber = candidates[currentIndex]; + combinationSumRecursive( + candidates, + remainingSum - currentNumber, + finalCombinations, + [...currentCombination, currentNumber], + currentIndex + ); + } + return finalCombinations; + } + } + } /** * Backtracking algorithm of finding all possible combination for specific sum.