title: Change given amount of money to bills
body: <p>Change the given amount of money into minimum number of bills.</p>
  
  <p>Inputs: 
  Amount: positive integer;
  Bills: a sorted list of distinct positive integers (e.g. [1, 5, 10]).</p>
  
  <p>Assumptions:
  Amount does not exceed 100.
  At most 4 bill values.</p>
  
  <p>Must return 0 if the amount cannot be changed.</p>
  
  <p>Examples: 
  Amount: 17, bills: [1, 5, 10], answer: 4 -> 10+5+1+1
  Amount: 17, bills: [2, 4], answer: 0</p>
  
  <p>Here's the code I have so far</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function sort(array) {
    for (var i = array.length - 1; i &gt;= 0; i--) {
      for (var j = 0; j &lt; i; j++) {
        if (array[j + 1] &gt; 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 &gt; 0) {
      for (var i = 0; i &lt; bills.length; i++) {
        if (amount &gt;= 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</code></pre>
  </div>
  </div>
  </p>
  
  <p>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.</p>
  
  <p>Would really appreciate suggestions, or fixed code. Please keep it fairly simple I am just a starter.</p>