forked from dwyl/book
-
Notifications
You must be signed in to change notification settings - Fork 24
/
cash.js
39 lines (33 loc) · 1.65 KB
/
cash.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
var C = {}; // C Object simplifies exporting the module
C.coins = [5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1];
/**
* getChange returns and Array containing the values of notes & coins
* equivalent to the change for a given transaction
* @param {Number} totalPayable the amount payable for a transaction
* @param {Number} cashPaid the amount the customer hands over to pay
* @returns {Array} [500,20,5] the array equivalent of the change
*/
C.getChange = function (totalPayable, cashPaid) {
'use strict';
// if(isNaN(totalPayable) || isNaN(cashPaid)) {
// throw new Error("totalPayable and cashPaid MUST both be numbers");
// }
var change = [], length = C.coins.length,
remaining = cashPaid - totalPayable; // we reduce this below
// console.log('\n TotalPayable: '+totalPayable +' | cashPaid: '+cashPaid);
// console.log('Difference: '+remaining +' ('+cashPaid+'-'+totalPayable+')');
for (var i = 0; i < length; i++) { // loop through array of notes & coins:
var coin = C.coins[i];
if(remaining/coin >= 1) { // check coin fits into the remaining amount
var times = Math.floor(remaining/coin); // no partial coins
// console.log('Coin: '+coin+' fits in '+remaining +' x ' +times);
for(var j = 0; j < times; j++) { // add coin to change array x times
change.push(coin);
remaining = remaining - coin; // reduce remaining amount by coin
}
}
}
// console.log(change); // print change array to console for feedback
return change
};
module.exports = C; // export the module with a single method