function getCombination(arr, n) {
const sortedArr = JSON.parse(JSON.stringify(arr)).sort();
const results = [];
const part = [];
for(let i = 0; i < sortedArr.length; i++) {
part.push(sortedArr[i]);
const summ = part.reduce((a, b) => a + b);
if (summ === n) {
results.push([...part]);
}
if (summ > n) {
part.pop();
i = sortedArr.indexOf(part[part.length - 1]);
part.pop();
}
}
return results;
}
console.log(getCombination([7, 8, 3, 4, 5, 6, 1, 2], 8));
// [[1, 2, 5], [1, 3, 4], [1, 7], [2, 6], [3, 5], [8]]