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
62
63
64
var log;
var cashArr = [
  ["PENNY",0.01],
  ["NICKEL",0.05],
  ["DIME",0.10],
  ["QUARTER",0.25],
  ["ONE",1],
  ["FIVE",5],
  ["TEN",10],
  ["TWENTY",20],
  ["ONE HUNDRED",100]
];
function checkCashRegister(price, cash, cid) {
  log = "";
  var arr =[];
  var change = cash-price;
  cid = cid.reverse();
  //log+=JSON.stringify(cid)+"\n\n";
  //log+= "price:"+price+"\ncash:"+cash+"\nchange:"+change+"\n\n";
  for(var i=0;i<cid.length;i++){    
    var $cid = cid[i][1];    
    var $cash = Cash(cashArr,cid[i][0]);
    //log+= "$cid:"+cid[i][0]+"="+$cid +"\n$cash:"+$cash+"\n";
    var c = Number.parseInt($cid/$cash);
    var d = Number.parseInt(change/$cash);
    if(d>c) d = c;
    
    //log+= "c:"+c+"\nd:"+d+"\n";
    if(change === 0) break;
    if(d>0 && $cid>0 && d*$cash<=change){
      var r = d*$cash;
      cid[i][1] -= r;
      change = (change - r).toFixed(2);
      arr.push([cid[i][0],r]);
      //log+="change:"+change+"\n";
      //log+= r+"$$$\n";
    }
    //log+="\n";
    //log+="|"+JSON.stringify(arr)+"|\n\n";
  }
  if(change === 0){
    for(var a=0;a<cid.length;a++){
      if(cid[a][1] > 0)
        return JSON.stringify(arr);
    }
    return "Closed";
  }
  return "Insufficient Funds";
}

function Cash(arrCid,cashType){
  for(var i=0;i<arrCid.length;i++){
    if(arrCid[i][0] == cashType)
      return arrCid[i][1];
  }
}
//checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);// should return an array.
 // checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);// should return a string.
  //checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);// should return a string.
checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);// should return [["QUARTER", 0.50]].
//checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);// should return [["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]].
  //checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);// should return "Insufficient Funds".
  //checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);// should return "Insufficient Funds".
  //checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);// should return "Closed".
Native Browser JavaScript