I'd like to find a way to combine in an array all possiblities between four percentages.
Results wanted :
possibilities = [[100,0,0,0],[99,1,0,0],[99,0,1,0],...,[0,0,1,99],[0,0,0,100]]
I'm using this function but it's very slow and doesn't seem to generate all possibilities.
combinePossibilities : function(a, min, max) {
var deferred = $q.defer();
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i){
rv['fund'+i] = arr[i];
}
return rv;
}
var fn = function(n, src, got, all) {
if (n === 0) {
if (got.length > 0) {
var total = 0;
angular.forEach(got, function(value){
total += value; //GET TOTAL OF THE COMBINATION
});
if(total === 100){
all.push(toObject(got));
}
}
return;
}
for (var j = 0; j < src.length; j++) {
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
}
return;
};
var all = [];
for (var i = min; i <= max; i++) {
console.log(a);
fn(i, a, [], all);
}
deferred.resolve(all);
return deferred.promise;
}
I found this function here Find all possible subset combos in an array? and modified it to only take in my array results equal to 100%.
Any clue ?
Thank you.