Created
May 31, 2017 03:53
-
-
Save khalid32/35db2c7d21ca79e1868519e7aaba9e49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument. | |
cid is a 2D array listing available currency. | |
Return the string "Insufficient Funds" if cash-in-drawer is less than the change due. Return the string "Closed" if cash-in-drawer is equal to the change due. | |
Otherwise, return change in coin and bills, sorted in highest to lowest order. | |
*/ | |
var deomination = [ | |
{name: 'ONE HUNDRED',val: 100.00}, | |
{name: 'TWENTY',val: 20.00}, | |
{name: 'TEN',val: 10.00}, | |
{name: 'FIVE',val: 5.00}, | |
{name: 'ONE',val: 1.00}, | |
{name: 'QUARTER',val: 0.25}, | |
{name: 'DIME',val: 0.10}, | |
{name: 'NICKEL',val: 0.05}, | |
{name: 'PENNY',val: 0.01} | |
]; | |
function checkCashRegister(price, cash, cid) { | |
var change = cash - price; | |
var register = cid.reduce(function(acc, curr) { | |
acc.total += curr[1]; | |
acc[curr[0]] = curr[1]; | |
return acc; | |
}, {total: 0}); | |
if (register.total === change) { | |
return 'Closed'; | |
} | |
if (register.total < change) { | |
return 'Insufficient Funds'; | |
} | |
var change_arr = deomination.reduce(function(acc, curr) { | |
var value = 0; | |
while (register[curr.name] > 0 && change >= curr.val) { | |
change -= curr.val; | |
register[curr.name] -= curr.val; | |
value += curr.val; | |
change = Math.round(change * 100) / 100; | |
} | |
if (value > 0) { | |
acc.push([ curr.name, value ]); | |
} | |
return acc; | |
}, []); | |
if (change_arr.length < 1 || change > 0) { | |
return "Insufficient Funds"; | |
} | |
return change_arr; | |
} | |
// Example cash-in-drawer array: | |
// [["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]] | |
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]]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment