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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function checkCashRegister(price, cash, cid) {
	
  var change = (cash - price);
  var funds = 0;
 
  for (var i=0; i<cid.length; i++) {
  	funds += cid[i][1];
  }

  
  if (funds < change) {
  	return "Insufficient Funds";
  } else if (funds == change) {
  	return "Closed";
  }
  

	var changePlaceholder = [
	[ 'PENNY', 0 ],
  [ 'NICKEL', 0 ],
  [ 'DIME', 0 ],
  [ 'QUARTER', 0 ],
  [ 'ONE', 0 ],
  [ 'FIVE', 0 ],
  [ 'TEN', 0 ],
  [ 'TWENTY', 0 ],
  [ 'ONE HUNDRED', 0 ]
  ];
  

  while (change !== 0) {
  	
  	if (change >= 100 && cid[8][1] != 0) {
  		change -= 100;
		cid[8][1] -= 100;
  		changePlaceholder[8][1] += 100;
  		  		change = change.toFixed(2)


  	} else if (change >= 20 && cid[7][1] != 0) {
  		change -= 20;
  		cid[7][1] -= 20;
  		changePlaceholder[7][1] += 20;
  		  		change = change.toFixed(2)


  	} else if (change >= 10 && cid[6][1] != 0) {
  		change -= 10;
  		cid[6][1] -= 10;
  		changePlaceholder[6][1] += 10;
  		change = change.toFixed(2)

  	} else if (change >= 5 && cid[5][1] != 0) {
  		change -= 5;
  		cid[5][1] -= 5;
  		changePlaceholder[5][1] += 5;
  		change = change.toFixed(2)

  	} else if (change >= 1 && cid[4][1] != 0) {
  		change -= 1;
  		cid[4][1] -= 1;
  		changePlaceholder[4][1] += 1;
  		change = change.toFixed(2)

  	} else if (change >= 0.25 && cid[3][1] != 0) {
  		change -= 0.25;
  		cid[3][1] -= 0.25;
  		changePlaceholder[3][1] += 0.25;
  		change = change.toFixed(2)

  	} else if (change >= 0.1 && cid[2][1] != 0) {
  		change -= 0.10;
  		cid[2][1] -= 0.10;
  		changePlaceholder[2][1] += 0.10;
  		change = change.toFixed(2)

  	} else if (change >= 0.05 && cid[1][1] != 0) {
  		change -= 0.05;
  		cid[1][1] -= 0.05;
  		changePlaceholder[1][1] += 0.05;
  		change = change.toFixed(2)

  	} else if (change >= 0.01 && cid[0][1] != 0) {
  		change -= 0.01;
  		cid[0][1] -= 0.01;
  		changePlaceholder[0][1] += 0.01;
  		change = change.toFixed(2)

  	} else {
	break;
}
  	
  	
  }
  
  if (change > 0) {return "Insufficient Funds"}
  
	changeArray = [];
  
   for (var j=0; j<changePlaceholder.length; j++) {
  	if (changePlaceholder[j][1] !== 0) {
  	changeArray.unshift(changePlaceholder[j]);
  	}
  }
  

return changeArray;
	
}



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]])

Native Browser JavaScript