lets say I have an array with different item-prices.
var myItemsEuro = [0.34, 0.11, 0.5, 0.33, 0.05, 0.13, 0.23, 3.22, 1.94]
I would like to have function like this:
function getTradeItems(0.89) { //The price of the item I want to buy
//Calculate, which of my items should be used to buy the item for 0.89€
return [0, 3, 6] //The position of my items in the array, which added together equal 0.90€
}
To clear things up:
I have a box of items with pricetags on them (myItemsEuro). I want to buy an item, using my items as a payment. The other party will accept my trade, if I overpay with atleast one cent.
The function should work, so i can pass the other guy's price to it (0.89 for example) and it returns, which items I will have to give away. The combination of these items must be above 0.89 cents (atleast 0.9), but should be as low as possible!
I am quite new to JS, and I was thinking about calculating every single combination of my items and then use the one that has the lowest difference to the buyprice. This seems really complicated to me and I don't even know how I would make it calculate every single combination and also save which items were used for the calculation.
Is there any way to achieve this a bit more efficient? I don't really expect any perfectly working code here, a little bit of help to get into the right direction would also be nice.
Any help is appreciated! :)
Edit:
Sorry for missing my own attempt. It's just that I have no idea how I should solve this at all. And no - not homework - this is supposed to be part of a chromeextension I am working on!
var myItemsEuro = [0.34, 0.11, 0.5, 0.33, 0.05, 0.13, 0.23, 3.22, 1.94]
function getTradeItems(marketPrice) {
var result = 0;
var positions = [];
for(i = 0; i < myItemsEuro.length; i++) {
result += myItemsEuro[i]; //add numbers from the array
positions.push(i); //save the used numbers position
if(result > marketPrice) { //if result is greater than marketPrice...
console.log(result)
console.log(positions)
return positions; //return positions in the array
}
}
}
getTradeItems(1.31);
Edit:
Sorting the array and then adding up numbers doesn't give a solution.
var x = 1.18;
//Sorted by numbers
var myItemsEuro = [0.05, 0.11, 0.13, 0.20, 0.35, 0.50, 0.60, 0.69, 0.75];
//Add together and stop when sum > x:
0.05 + 0.11 + 0.13 + 0.20 + 0.35 + 0.50 = 1.34
//Best solution would be adding [6] and [8] from the array
0.50 + 0.69 = 1.19
You could use a brute force approach and test all combinations of the items if they are greater or equal of the
target
.The base consideration is to take a counter from zero up to 2values.length and check if the actual 2index is part of the counter with a bitwise AND
&
. If so, take the value from the index and put it into theparts
array.Then build the
sum
of theparts
and check ifsum
is greater or equal oftarget
and possibly smaller thansum
inresult
array, then move the result to the result array. Ifsum
is equal toresult[0].sum
, then push the actualparts
andsum
to theresult
.This proposal works with unsorted values, but could be more efficient, if the values which are greater then the
target
value are not included in the array to work on.Another draw back is the fact, that bitwise arithmetic works only with 32 bit, that means arrays with more items than 32 are not possible use.