2

Change the given amount of money into minimum number of bills.

Inputs: Amount: positive integer; Bills: a sorted list of distinct positive integers (e.g. [1, 5, 10]).

Assumptions: Amount does not exceed 100. At most 4 bill values.

Must return 0 if the amount cannot be changed.

Examples: Amount: 17, bills: [1, 5, 10], answer: 4 -> 10+5+1+1 Amount: 17, bills: [2, 4], answer: 0

Here's the code I have so far

function sort(array) {
  for (var i = array.length - 1; i >= 0; i--) {
    for (var j = 0; j < i; j++) {
      if (array[j + 1] > array[j]) {
        var z = array[j];
        array[j] = array[j + 1];
        array[j + 1] = z;
      }
    }
  }
  return array;
}



function change(amount, bills) {
  sort(bills);
  var result = [];
  while (amount > 0) {
    for (var i = 0; i < bills.length; i++) {
      if (amount >= bills[i]) {
        amount -= bills[i];
        result.push(bills[i]);
        i--;
      }
    }
  }
  return result.length;
}
console.log(change(17, [1, 5, 10])); // Expect: 4
console.log(change(17, [2, 4])); // Expect: 0
console.log(change(18, [2, 4])); // Expect: 5
//console.log(change(17, [3, 5])); // Expect: 5

There are 2 problems One is that if the amount cannot be divided it doesn't return 0 but just lags out because it's an infinite loop. Second is that in the last example, 17,[3,5] my code takes the 5 3 times and then realises that it can't do the remaining 2 and lags out, instead of doing 3 4 times and adding a 5.

Would really appreciate suggestions, or fixed code. Please keep it fairly simple I am just a starter.

Mendal
  • 29
  • 3

2 Answers2

0

If fixed your change function and added comments to explain my changes, let me know if you have any doubts

function change (amount, bills) {
    //Asign sorted array of bills to possibleBills
    var possibleBills = sort(bills);

    var result = [];

    //Asign amount to currentAmount
    var currentAmount = amount;

    //Sort through possibleBills
    for (var i = 0; i < possibleBills.length; i++) {
        //Perform action inside while loop if the current bill value can be substracted from currentAmount
        while (currentAmount - possibleBills[i] >= 0) {

            currentAmount -= possibleBills[i];
            result.push(possibleBills[i]);

            //End loop and return the length of result if currentAmount reaches 0
            if (currentAmount === 0) {
                return result.length;
            }
        }
    }

    //Return 0 if the amount couldn't be changed with the given bills
    if (currentAmount > 0) {
        return 0;
    }


    return result.length;
};
Larpee
  • 800
  • 1
  • 7
  • 18
  • 1
    It works better than mine but there is still the problem of the last one. With 17, [3,5] it returns 0 as if it can't do it, but it is possible by taking 4 3s and 1 5 – Mendal Feb 26 '17 at 21:44
  • comments in code should explain *why* the code is doing what it's doing. It is really annoying to see people put comments all over the place explaining things that the code is doing when the code explains itself better – Scott Selby Feb 27 '17 at 01:36
  • If you actually read what I wrote, and didn't just rage comment. You would have noticed that I am a starter. And Larpee nicely took that into concideration and added comments so that I understand what he was doing. It helped me, and I don't know why you are mad about it. – Mendal Feb 27 '17 at 02:27
  • @Mendal I'm glad I could help. I'm not sure about how you can fix the last one, I'm sorry – Larpee Feb 27 '17 at 02:53
0

function change(amount, bills) {
  const billsDesc = bills.sort((a, b) => b - a);

  const give = {}
  let remaining = amount;
  for (const bill of billsDesc) {
    const qty = Math.floor(remaining/bill);
    give[bill] = qty;
    remaining -= qty*bill;
  }

  give.totalQty = Object.values(give).reduce((curr, prev) => curr + prev, 0);

  return remaining === 0? give.totalQty : 0;
}

console.log(`${change(17, [1, 5, 10])} should equal 4`);
console.log(`${change(17, [2, 4])} should equal 0`);
console.log(`${change(18, [2, 4])} should equal 5`);
Santiago
  • 331
  • 5
  • 8