This question already has an answer here:
- Find the highest subset of an integer array whose sums add up to a given target 1 answer
I'm stumbling into a problem that I've to treat a given value and check if this value is bigger than my array of values, if it is, combine the output using my array.
For example.
My array always will be:
const ArrayPrimitive = [100,50,20,10];
And for example the given value in the input:
- Entry: 30.00 Result: [20.00, 10.00]
- Entry: 80.00 Result: [50.00, 20.00, 10.00]
- Entry: 125.00 Result: throw NoteUnavailableException
- Entry: -130.00 Result: throw InvalidArgumentException
- Entry: NULL Result: [Empty Set]
I've started to develop but I'm stuck in the in how to deal with the rest, after check if the value is valid.
const ArrayPrimitive = [100,50,20,10];
var combination = [];
$(document).ready(function(){
$(".btn-submit").on("click",function(){
var amountOfMoney = document.getElementById('amountOfMoney').value;
for(let i=0; i=ArrayPrimitive.length;i++){
if(amountOfMoney=ArrayPrimitive[i]){
combination.push(amountOfMoney);
}
while(amountOfMoney > ArrayPrimitive[i]){
var k = amountOfMoney/ArrayPrimitive[i];
for(let j=0; j = k; j++){
combination.push(ArrayPrimitive[i]);
amountOfMoney = amountOfMoney - ArrayPrimitive[i];
}
var rest = n % ArrayPrimitive[i];
}
}
});
});
My major questions are:
- Can you guys help me to solve this in the old way?
- I'm about to learn ES6 and probably the
.map
or other function could save my life, could someone enlighten me in this point?
I hope I made myself clear.
https://jsfiddle.net/v748ha08/1/
UPDATE: Thanks @Malk for your answer but your solution just provides the right answer only when you don't have to combine multiple values of the subset.
e.g. Entry: 200.00 Result: [100.00, 100.00]
In this case I need 2 values of 100 in my subset, and when I test this they functions throws me an error.