A counter keeping track of multiple values inside an array
arrays, combinations, counter, javascript
Solution
Basically this proposal relies on a function to get the next items
getItems = function () {
var price = 0,
array = lottery.map(function (a) { return a.price; });
return function () {
var items;
do {
items = combine(array, price);
price++;
} while (!items.length)
return items;
}
}(),
which starts at price with zero and increments the value by one until a combination of items is found. Then the `items` array is returned. The function works as generator.
The other important function is the combination of items with a given price and the try to get an array with the items.
function combine(array, sum) {
function c(left, right, sum) {
if (!sum) {
result = right;
return true;
}
return left.some(function (a, i, aa) {
return a <= sum && c(aa.slice(i + (a > sum - a)), right.concat(a), sum - a);
});
}
var result = [];
c(array.sort(function (a, b) { return b - a; }), [], sum);
return result;
}
`combine` takes an array with prices and a wanted sum to reach with combinating the given prices. If successfull, an array with the items is returned, otherwise an empty array.
The third part is to use the items as long as the investment is not negative. If that happens, a new items set is fetched.
function combine(array, sum) {
function c(left, right, sum) {
if (!sum) {
result = right;
return true;
}
return left.some(function (a, i, aa) {
return a <= sum && c(aa.slice(i + (a > sum - a)), right.concat(a), sum - a);
});
}
var result = [];
c(array.sort(function (a, b) { return b - a; }), [], sum);
return result;
}
var lottery = [{ name: 'twig', price: 5, win: 40 }, { name: 'rock', price: 10, win: 80 }, { name: 'shell', price: 28, win: 250 }, { name: 'chip', price: 50, win: 400 }, { name: 'gold', price: 56, win: 500 }, { name: 'diamond', price: 280, win: 2500 }],
lotteryByPrice = lottery.reduce(function (r, a) { r[a.price] = a; return r; }, Object.create(null)),
getItems = function () {
var price = 0,
array = lottery.map(function (a) { return a.price; });
return function () {
var temp;
do {
temp = combine(array, price);
price++;
} while (!temp.length)
return temp;
}
}(),
createTableRow = function (element) {
var table = document.createElement('table'),
tr = document.createElement('tr');
['Game', 'Items', 'Types', 'Spend Per Game', 'Total Spend', 'Max. Possible Winnigs', 'Investment'].forEach(function (a) {
var th = document.createElement('th');
th.appendChild(document.createTextNode(a));
tr.appendChild(th);
});
table.appendChild(tr);
element.appendChild(table);
return function (row) {
var tr = document.createElement('tr');
['game', 'items', 'types', 'spend', 'total', 'potential', 'investment'].forEach(function (k) {
var td = document.createElement('td');
td.appendChild(document.createTextNode(row[k]));
tr.appendChild(td);
});
if (row.topBorder) {
tr.style.borderTop = '2px solid #666';
}
table.appendChild(tr);
};
}(document.body),
row = { game: null, items: null, types: null, spend: null, total: 0, potential: null, investment: null },
i,
items = getItems(),
add = function (a, b) { return a + b; },
winP = function (a) { return lotteryByPrice[a].win; },
nameP = function (a) { return lotteryByPrice[a].name; };
for (i = 1; i <= 70; i++) {
row.topBorder = false;
while (row.total - items.reduce(add) + items.map(winP).reduce(add) < 0) {
items = getItems();
row.topBorder = true;
}
row.game = i;
row.items = items.length;
row.types = items.map(nameP).join(' + ');
row.spend = -items.reduce(add);
row.total += row.spend;
row.potential = items.map(winP).reduce(add);
row.investment = row.potential + row.total;
createTableRow(row);
}
table { border-collapse: collapse; font-family: Sans-Serif; }
th { border: 1px solid #ccc; padding: 0 10px; }
td { text-align: center; border: 1px solid #ccc; }
Problem
This seems sort of complex so I'll do my best to be as clear as possible.The particular function I'm looking for dynamically creates a money spent | money won chart for a game of gambling. I have a lottery of sorts that the user can bet on. There are 6 items the user can buy which each have 6 prizes: These can be put into objects or arrays. `var prices = [5,10,28,50,56,280]`. `var possibleWins = [40,80,250,400,500,2500]` I'm trying to create a chart that calculates how much money you would have to spend on each particular item per game to guarantee you gain money - for 300 games out. So here is an example of how the chart should start off: investment = max possible winnings + total spent( which is negative ) The 2nd row is assuming the first game already happened and lost. And so on. The idea is to start with the smallest item but give up once it can no longer get you positive even if you win. This is why on row 9 we switch to the rock. ( our investment is at 0 and if we play a twig again, the most we can win is 40. So even if we did win, we would actually have lost 5 overall. ) Also worth pointing out is that if you win on 1 item; you win on all items for that particular game. So you get all prizes combined. I've been working on this for a few days now and some of these related questions have my initial attempts ( but I honestly have no idea ): How to find the lowest possible combination of keys within an array Counter that generates the lowest sum from a combination of indexes above the previous value Add an arrays keys to themselves until exceeding a limit? EDIT: At least 1 item(s) must be bought every game and games cannot be skipped