Created
January 30, 2017 23:51
-
-
Save gatezh/40e5a45665504f08a7fea2329de5bbee to your computer and use it in GitHub Desktop.
Free Code Camp > Advanced Algorithm Scripting > Exact Change
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 checkCashRegister(price, cash, cid) { | |
var bills = {"PENNY": 0.01, | |
"NICKEL": 0.05, | |
"DIME": 0.10, | |
"QUARTER": 0.25, | |
"ONE": 1, "FIVE": 5, | |
"TEN": 10, | |
"TWENTY": 20, | |
"ONE HUNDRED": 100}; | |
var total = function(array) { | |
var result = 0; | |
for (var i = 0; i < array.length-1; i++) { | |
result += array[i][1]; | |
} | |
return result; | |
} | |
var totalChange = cash - price; | |
var totalCID = total(cid); | |
var change = []; | |
if (totalCID === totalChange) | |
return "Closed"; | |
for (var i = cid.length-1; i >= 0; i--) { | |
var numOfBills = Math.floor(totalChange / bills[cid[i][0]]); | |
if (numOfBills > 0 && cid[i][1] >= bills[cid[i][0]] * numOfBills) { | |
change.push([cid[i][0], bills[cid[i][0]] * numOfBills]); | |
// have some problem here | |
// in debugger after first run of the next line | |
// totalResult is 0 for some reason. What's the reason? | |
var totalResult = total(change); | |
if (totalResult === totalChange) { | |
return change; | |
} | |
} | |
} | |
var totalResult = total(change); | |
if (totalResult < totalChange) | |
return "Insufficient Funds"; | |
} | |
// 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]] | |
// TESTS | |
// (typeof 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]]) === "array") ? console.log("...OK") : console.log("FAIL: must be type \"array\""); | |
// (typeof checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) === "string") ? console.log("...OK") : console.log("FAIL: must be a string"); | |
// (typeof checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) === "string") ? console.log("...OK") : console.log("FAIL: must be a string"); | |
(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]]) === [["QUARTER", 0.50]]) ? console.log("...OK") : console.log("FAIL: must be [[\"QUARTER\", 0.50]]"); | |
// (checkCashRegister(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.00], ["ONE", 1.00], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]]) ? console.log("...OK") : console.log('FAIL: must be [["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]]'); | |
// (checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) === "Insufficient Funds") ? console.log("...OK") : console.log('FAIL: must be "Insufficient Funds"'); | |
// (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]]) === "Insufficient Funds") ? console.log("...OK") : console.log('FAIL: must be "Insufficient Funds"'); | |
// (checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) === "Closed") ? console.log("...OK") : console.log('FAIL: must be "Closed"'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment