JSFiddle - React, Tailwind, and code Playground
by Givi Kesanashvili
JavaScript
(function () {
"use strict";
var numbers = [2, 25, 37, 54, 54, 76, 88, 91, 99],
combinations = [];
(function () {
var temp = [],
len = numbers.length,
sum = 0;
for (var i = 0; i < len; i++) {
temp.length = 0;
sum = numbers[i];
if (sum < 100) {
temp.push(sum);
add(temp);
for (var j = 0; j < len; j++) {
if (numbers[j] >= 100 || i === j) {
continue;
}
sum += numbers[j];
if (sum < 100) {
temp.push(numbers[j]);
add(temp);
} else {
sum -= numbers[j];
}
}
}
}
}());
function add(val) {
var contains = false,
temp = null;
val.sort(function (a, b) {
return a - b;
});
temp = val.join(" ");
if (combinations.length === 0) {
combinations.push(temp.split(" "));
return;
}
for (var i = 0; i < combinations.length; i++) {
if (combinations[i].join(" ") === temp) {
contains = true;
}
}
if (!contains) {
combinations.push(temp.split(" "));
}
}
console.log(combinations);
}());