@rscales02/

exact change fcc

JavaScript

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.4286561969747873","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"ZnVuY3Rpb24gY2hlY2tDYXNoUmVnaXN0ZXIocHJpY2UsIGNhc2gsIGNpZCkgew0KICB2YXIgY2hhbmdlID0gW10sDQogICAgY2hhbmdlRHVlID0gY2FzaCAtIHByaWNlLA0KICAgIGtleSA9IFsNCiAgICAgIFsnT05FIEhVTkRSRUQnLCAxMDBdLA0KICAgICAgWydUV0VOVFknLCAyMF0sDQogICAgICBbJ1RFTicsIDEwXSwNCiAgICAgIFsnRklWRScsIDVdLA0KICAgICAgWydPTkUnLCAxXSwNCiAgICAgIFsnUVVBUlRFUicsIDAuMjVdLA0KICAgICAgWydESU1FJywgMC4xXSwNCiAgICAgIFsnTklDS0VMJywgMC4wNV0sDQogICAgICBbJ1BFTk5ZJywgMC4wMV0sDQogICAgXTsNCiAgY2lkLnJldmVyc2UoKTsgLy9wdXRzIGNhc2ggZHJhd2VyIGluIHNhbWUgb3JkZXIgYXMga2V5DQogIGZvciAodmFyIGkgPSAwOyBpIDwga2V5Lmxlbmd0aDsgaSsrKSB7IC8vY3ljbGVzIHRocm91Z2ggY2FzaGRyYXdlci9rZXkNCiAgICAgIC8vY2hlY2tzIGlmIGN1cnJlbnQgZGVub21pbmF0aW9uIGlzIGFwcHJvcHJpYXRlIHRvIGdpdmUgY2hhbmdlIGFuZCBpcyBwcmVzZW50IGluIGRyYXdlcg0KICAgIGlmIChjaGFuZ2VEdWUgLSBrZXlbaV1bMV0gPj0gMCAmJiBjaWRbaV1bMV0gPiAwKXsgDQogICAgICAgIC8vIHB1c2hlcyBjdXJyZW50IGRlbm9taW5hdGlvbiBpbnRvIGNoYW5nZSBnaXZlbg0KICAgICAgY2hhbmdlLnB1c2goa2V5W2ldKTsgDQogICAgICAgIC8vYWxsb3dzIGFjY2VzcyB0byBjdXJyZW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zdWVzOg0KICAgICAgMS4gY2lkIHNob3VsZCBuZXZlciBnbyBuZWdhdGl2ZSwgYnV0IGl0IGRvDQogICAgICAyLiBzZXZlcmFsIHZhbHVlcyBvZiBlYWNoIGRlbm9taW5hdGlvbiBhcHBlYXJzIHRvIGRpc2FwcGVhcg0KICAgICAgMy4gd2h5IHRoZSBmdWNrIGlzIHRoaXMgZXN0aW1hdGluZyEhISEhIQ0KICAgICAgKi8NCiAgICB9DQogIH0NCiAgY29uc29sZS5sb2coY2lkKQ0KICByZXR1cm4gY2hhbmdlOw0KfQ0KDQpjaGVja0Nhc2hSZWdpc3RlcigzLjI2LCAxMDAuMCwgWw0KICBbJ1BFTk5ZJywgMS4wMV0sDQogIFsnTklDS0VMJywgMi4wNV0sDQogIFsnRElNRScsIDMuMV0sDQogIFsnUVVBUlRFUicsIDQuMjVdLA0KICBbJ09ORScsIDkwLjBdLA0KICBbJ0ZJVkUnLCA1NS4wXSwNCiAgWydURU4nLCAyMC4wXSwNCiAgWydUV0VOVFknLCA2MC4wXSwNCiAgWydPTkUgSFVORFJFRCcsIDEwMC4wXSwNCl0pOw0KDQovL1tbIlRXRU5UWSIsIDYwLjAwXSwgWyJURU4iLCAyMC4wMF0sIFsiRklWRSIsIDE1LjAwXSwgWyJPTkUiLCAxLjAwXSwgWyJRVUFSVEVSIiwgMC41MF0sIFsiRElNRSIsIDAuMjBdLCBbIlBFTk5ZIiwgMC4wNF1dDQo="},"asBuffer":null},"loaded":true}}
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