MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / backtrack

Function backtrack

backtracking/target-sum.js:20–32  ·  view source on GitHub ↗
(idx, currSum, currSumArray, nums, result, target)

Source from the content-addressed store, hash-verified

18};
19
20function backtrack(idx, currSum, currSumArray, nums, result, target) {
21 if (idx > nums.length || currSum > target) {
22 return;
23 }
24 if (currSum === target) {
25 result.push(currSumArray.slice());
26 return;
27 }
28 currSumArray.push(nums[idx]);
29 backtrack(idx + 1, currSum + nums[idx], currSumArray, nums, result, target);
30 currSumArray.pop();
31 backtrack(idx + 1, currSum, currSumArray, nums, result, target);
32}
33
34const nums = [1, 2, 3, 4, 5, 6, 7, 8];
35console.table(getSums(nums, 8));

Callers 1

getSumsFunction · 0.70

Calls 2

pushMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected