I have an array with this layout:
const arrayOfSections = [{chance: 100, items: [1,2,3,4]}, {chance: 49, items: [7,9]}, {chance: 100, items: [0,5]}];
And I am trying to find the best way in just javascript no libraries to basically find all possible combinations of the items value in each array inside arrayOfSections, while also taking into consideration the chance percentage that is in the chance key for that specific array. So for example, if there is a 49% chance for the array with the items value [7,9], it would only generate the possible combinations with only 49% of the combinations having the items inside this array.
I am currently using this function here:
const allPossibleCases = (arraysToCombine) => {
console.log(arraysToCombine);
const divisors = [];
let permsCount = 1;
for (let i = arraysToCombine.length - 1; i >= 0; i--) {
divisors[i] = divisors[i + 1]
? divisors[i + 1] * arraysToCombine[i + 1].items.length
: 1;
permsCount *= arraysToCombine[i].length || 1;
}
let totalOutputs = permsCount;
const getCombination = (n, arrays, divisors) =>
arrays.reduce((acc, arr, i) => {
acc.push(arr[Math.floor(n / divisors[i]) % arr.length]);
return acc;
}, []);
const combinations = [];
for (let i = 0; i < permsCount; i++) {
combinations.push(getCombination(i, arraysToCombine, divisors));
}
return combinations;
};
To basically find all the possible combinations based on just
const arraysToCombine = [[1,2,3,4], [7,9], [0,5]]
and its working fine but im having a really hard time figuring out how I could do this while also taking into consideration the chance percentages.
Hopefully this isnt too sloppy of a post and I appreciate any help lol.