Last active
August 29, 2015 14:16
-
-
Save aldraco/5c6c944109ed37db171b to your computer and use it in GitHub Desktop.
cash register challenge
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
function drawer(price, payment, cid) { | |
var change = payment - price; | |
function findCID (cid) { | |
var total = 0; | |
cid.forEach(function(value, index) { | |
total += value[1]; | |
}); | |
return total; | |
} | |
var totalCID = findCID(cid); | |
if (change > totalCID) { | |
return "Insufficient Funds"; | |
} | |
if (change === totalCID) { | |
return "Closed"; | |
} | |
var changeBills = function findChange (change) { | |
var count; | |
var regDrawer = []; | |
var reference = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01]; | |
var moneyNames = ['ONE HUNDRED', 'TWENTY', 'TEN', 'FIVE', 'ONE', 'QUARTER', 'DIME', 'NICKEL', 'PENNY']; | |
reference.forEach(function(value, index) { | |
// filter for each size bill | |
if (change >= value) { | |
// make sure there's enough of this denomination | |
if (change < cid[8-index][1]) { | |
count = Math.floor(change.toFixed(2)/value).toFixed(2); | |
regDrawer.push([moneyNames[index], (count*reference[index]).toFixed(2)]); | |
change = (change % value); | |
} else { | |
//take out all the twenties | |
change -= (cid[8-index][1]).toFixed(2); | |
regDrawer.push([moneyNames[index], (cid[8-index][1]).toFixed(2)]); | |
} | |
} | |
}); | |
return regDrawer; | |
}; | |
return changeBills(change); | |
} | |
drawer(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]]); | |
//[['TWENTY', 60.00], ['TEN', 20.00], ['FIVE', 15], ['ONE', 1], ['QUARTER', 0.50], ['DIME', 0.20], ['PENNY', 0.04] ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment