Store values in an array after treat the values fr

2019-07-26 07:15发布

问题:

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:

  1. Can you guys help me to solve this in the old way?
  2. 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.

回答1:

If you want to iterate through an array and build up an object (or anything else) along the way then Array.reduce() is the way to go.

const ArrayPrimitive = [100, 95, 20, 10]; // Assuming presorted array(descending)

function findChange(m) {
  return ArrayPrimitive.reduce((mm, c) => {
    if (mm.rest >= c) {
      mm.change.push(c);
      mm.rest -= c
    }
    return mm
  }, {
    change: [],
    rest: m
  });
}

function findChangeOld(m) {
  var retval = {
      change: [],
      rest: m
    },
    i = ArrayPrimitive.length;

  for (var x = 0; x < i; x++) {
    if (retval.rest >= ArrayPrimitive[x]) {
      retval.change.push(ArrayPrimitive[x])
      retval.rest -= ArrayPrimitive[x];
    }
  }
  return retval;
}

function calcChange(v) {
  var c = findChangeOld(v);

  if (v < 0 || isNaN(v)) {
    console.log(`${v}: throw InvalidArgumentException`);
    return;
  }

  if (c.rest > 0)
    console.log(`${v}: throw NoteUnavailableException`);
  else
    console.log(`${v}: ${c.change}`);
}

calcChange(30);
calcChange(80);
calcChange(105);
calcChange(125);
calcChange(-130);
calcChange(null);