-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
111 lines (96 loc) · 3.11 KB
/
solution.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
let cashBox = [
{ 50: 2 },
{ 20: 10 },
{ 10: 0 },
{ 5: 4 },
{ 2: 0 },
{ 1: 0 },
{ 0.5: 25 },
{ 0.2: 5 },
{ 0.1: 25 },
{ 0.05: 25 },
{ 0.02: 25 },
{ 0.01: 25 },
];
/**
* this function returns the total amount of
* notes and coins available in cashbox
* @param {the cashier cashBox} cashBox
* @returns
*/
function checkCashBox(cashBox) {
let cashBoxTotal = cashBox.reduce(function (accumulator, currentValue) {
for (key in currentValue) {
return currentValue[key] * key + accumulator;
}
}, 0);
return cashBoxTotal;
}
function createCashCounter() {
return function (price, paidCash) {
const cashNotes = [50, 20, 10, 5, 2, 1];
const cashCoins = [0.5, 0.2, 0.1, 0.05, 0.02, 0.01];
let cashRegister = [];
let change = Number(paidCash - price);
if (paidCash == price) {
return "Payment done print the receipt?";
}
if (paidCash < price) {
return price - paidCash + " more euro should be paid";
}
if (checkCashBox(cashBox) < change) {
return "We don't have enough in the cash box";
}
if (checkCashBox(cashBox) == 0) {
return "Empty cash box";
}
cashNotes.forEach((note) => {
if (change >= note) {
// to get a cashBox object by note
const cashObject = cashBox.find(
(cashBoxObj) => Object.keys(cashBoxObj) == note
);
let quotientNote = Number(Math.floor(change / note));
if (cashObject[note] < 1 && cashObject[note] - quotientNote < 0) {
return;
}
//update the cashBox
cashBox.forEach((item) =>
item[note] ? (item[note] -= quotientNote) : item
);
let result = { [`${note} Euro`]: quotientNote };
cashRegister.push(result);
change = change % note;
}
});
cashCoins.forEach((coin) => {
let quotientCoin = Number(Math.floor(change / coin));
if (change >= coin) {
// to get a cashBox object by coin
const coinObject = cashBox.find(
(cashBoxObj) => Object.keys(cashBoxObj) == coin
);
if (coinObject[coin] < 1 && coinObject[coin] - quotientCoin < 0) {
return; //when note 0 return
}
//update cashbox whenever their is a change
cashBox.forEach((item) =>
item[coin] ? (item[coin] -= quotientCoin) : item
);
let result = { [`${coin} cent`]: quotientCoin };
cashRegister.push(result);
change = change % coin;
}
});
return cashRegister;
};
}
const cashCounter = createCashCounter(); //function expression
console.log(cashCounter(15, 15)); //Payment done print the receipt?
console.log(cashCounter(15, 10)); //5 more euro should be paid
console.log(cashCounter(100, 500)); //We don't have enough in the cash box
console.log(cashCounter(22, 23)); //[ { '0.5 cent': 2 } ]
console.log(cashCounter(5, 100)); //[ { '50 Euro': 1 }, { '20 Euro': 2 }, { '5 Euro': 1 } ]
console.log(cashCounter(4.5, 5)); //[ { '0.5 cent': 1 } ]
console.log(cashCounter(350, 400)); //[ { '50 Euro': 1 } ]
console.log(cashCounter(15, 100)); //[ { '50 Euro': 1 }, { '20 Euro': 1 }, { '5 Euro': 3 } ]