@rscales02/

exact change fcc

JavaScript

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function checkCashRegister(price, cash, cid) {
  var change = [],
    changeDue = cash - price,
    key = [
      ['ONE HUNDRED', 100],
      ['TWENTY', 20],
      ['TEN', 10],
      ['FIVE', 5],
      ['ONE', 1],
      ['QUARTER', 0.25],
      ['DIME', 0.1],
      ['NICKEL', 0.05],
      ['PENNY', 0.01],
    ];
  cid.reverse(); //puts cash drawer in same order as key
  for (var i = 0; i < key.length; i++) { //cycles through cashdrawer/key
      //checks if current denomination is appropriate to give change and is present in drawer
    if (changeDue - key[i][1] >= 0 && cid[i][1] > 0){ 
        // pushes current denomination into change given
      change.push(key[i]); 
        //allows access to current denomination in change
      var x = change.length - 1; 
        //removes 1 value of denomination from cid after push to change
      cid[i][1] -= key[i][1];
        //removes 1 value of denomination from changedue after push to change
      changeDue -= key[i][1];
        //should maintain current denomination until changeDue is less than current denomination or cid for denomination is empty
      while (changeDue - key[i][1] >= 0 && cid[i][1] > 0){
          //removes 1 value of current denomination from changeDue
        changeDue -= key[i][1];
        console.log(changeDue);
          //add 1 value of current denomination to change
        change[x][1] += key[i][1];
          //remove 1 value of current denomination from cid
        cid[i][1] -= key[i][1];
      }
      /*current issues:
      1. cid should never go negative, but it do
      2. several values of each denomination appears to disappear
      3. why the fuck is this estimating!!!!!!
      */
    }
  }
  console.log(cid)
  return change;
}

checkCashRegister(3.26, 100.0, [
  ['PENNY', 1.01],
  ['NICKEL', 2.05],
  ['DIME', 3.1],
  ['QUARTER', 4.25],
  ['ONE', 90.0],
  ['FIVE', 55.0],
  ['TEN', 20.0],
  ['TWENTY', 60.0],
  ['ONE HUNDRED', 100.0],
]);

//[["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]]
Native Browser JavaScript