-
-
Save anonymous/1bfaded903443b2df7dd to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/vmlinz 's solution for Bonfire: Exact Change
This file contains hidden or 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
// Bonfire: Exact Change | |
// Author: @vmlinz | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-exact-change?solution=function%20drawer(price%2C%20cash%2C%20cid)%20%7B%0A%20%20var%20cashes%20%3D%20%5B%5B%22PENNY%22%2C%200.01%5D%2C%20%5B%22NICKEL%22%2C%200.05%5D%2C%20%5B%22DIME%22%2C%200.10%5D%2C%20%5B%22QUARTER%22%2C%200.25%5D%2C%20%5B%22ONE%22%2C%201.00%5D%2C%20%5B%22FIVE%22%2C%205.00%5D%2C%20%5B%22TEN%22%2C%2010.00%5D%2C%20%5B%22TWENTY%22%2C%2020.00%5D%2C%20%5B%22ONE%20HUNDRED%22%2C%20100.00%5D%5D.reduce(function%20(hash%2C%20elem%2C%20index)%20%7B%0A%20%20%20%20hash%5Belem%5B0%5D%5D%20%3D%20elem%5B1%5D%3B%0A%20%20%20%20return%20hash%3B%0A%20%20%7D%2C%20%7B%7D)%3B%0A%20%20var%20change%3B%0A%20%20%2F%2F%20Here%20is%20your%20change%2C%20ma%27am.%0A%20%20var%20result%20%3D%20cash%20-%20price%3B%0A%20%20var%20total%20%3D%20cid.reduce(function%20(p%2C%20c)%20%7B%0A%20%20%20%20return%20p%20%2B%20c%5B1%5D%3B%0A%20%20%7D%2C%200.0).toFixed(2)%3B%0A%0A%20%20if%20(result%20%3E%20total)%0A%20%20%20%20return%20%22Insufficient%20Funds%22%3B%0A%20%20else%20if%20(result.toFixed(2)%20%3D%3D%3D%20total)%0A%20%20%20%20return%20%22Closed%22%3B%0A%0A%20%20change%20%3D%20cid.reverse().reduce(function%20(p%2C%20c)%20%7B%0A%20%20%20%20if%20(result%20%3E%20cashes%5Bc%5B0%5D%5D)%20%7B%0A%20%20%20%20%20%20%2F%2F%201.%20get%20the%20change%20needed%20for%20this%20round%0A%20%20%20%20%20%20var%20needed_change%20%3D%20result%20-%20result%20%25%20cashes%5Bc%5B0%5D%5D%3B%0A%20%20%20%20%20%20%2F%2F%202.%20get%20the%20minimal%20change%20from%20needed%20and%20maximium%20of%20cash%20available%0A%20%20%20%20%20%20var%20current_change%20%3D%20needed_change%20%3C%20c%5B1%5D%20%3F%20needed_change%20%3A%20c%5B1%5D%3B%0A%20%20%20%20%20%20%2F%2F%203.%20get%20the%20rest%20needed%20to%20calculate%20changes%0A%20%20%20%20%20%20result%20%3D%20Math.round((result%20-%20current_change)%20*%20100.0)%20%2F%20100%3B%0A%20%20%20%20%20%20%2F%2F%204.%20push%20current%20change%20to%20the%20out%20result%0A%20%20%20%20%20%20p.push(%5Bc%5B0%5D%2C%20current_change%5D)%3B%0A%20%20%20%20%20%20console.log(needed_change%2C%20current_change%2C%20result)%3B%0A%20%20%20%20%20%20return%20p%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%20%20return%20p%3B%0A%20%20%7D%2C%20%5B%5D)%3B%0A%0A%20%20console.log(result.toFixed(2)%2C%20total%2C%20change)%3B%0A%0A%20%20if%20(result.toFixed(2)%20!%3D%3D%20%270.00%27)%0A%20%20%20%20return%20%22Insufficient%20Funds%22%3B%0A%0A%20%20return%20change%3B%0A%7D%0A%2F%2F%20Example%20cash-in-drawer%20array%3A%0A%2F%2F%20%5B%5B%22PENNY%22%2C%201.01%5D%2C%0A%2F%2F%20%5B%22NICKEL%22%2C%202.05%5D%2C%0A%2F%2F%20%5B%22DIME%22%2C%203.10%5D%2C%0A%2F%2F%20%5B%22QUARTER%22%2C%204.25%5D%2C%0A%2F%2F%20%5B%22ONE%22%2C%2090.00%5D%2C%0A%2F%2F%20%5B%22FIVE%22%2C%2055.00%5D%2C%0A%2F%2F%20%5B%22TEN%22%2C%2020.00%5D%2C%0A%2F%2F%20%5B%22TWENTY%22%2C%2060.00%5D%2C%0A%2F%2F%20%5B%22ONE%20HUNDRED%22%2C%20100.00%5D%5D%0A%0Adrawer(19.50%2C%2020.00%2C%20%5B%5B%22PENNY%22%2C%201.01%5D%2C%20%5B%22NICKEL%22%2C%202.05%5D%2C%20%5B%22DIME%22%2C%203.10%5D%2C%20%5B%22QUARTER%22%2C%204.25%5D%2C%20%5B%22ONE%22%2C%2090.00%5D%2C%20%5B%22FIVE%22%2C%2055.00%5D%2C%20%5B%22TEN%22%2C%2020.00%5D%2C%20%5B%22TWENTY%22%2C%2060.00%5D%2C%20%5B%22ONE%20HUNDRED%22%2C%20100.00%5D%5D)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function drawer(price, cash, cid) { | |
var cashes = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.10], ["QUARTER", 0.25], ["ONE", 1.00], ["FIVE", 5.00], ["TEN", 10.00], ["TWENTY", 20.00], ["ONE HUNDRED", 100.00]].reduce(function (hash, elem, index) { | |
hash[elem[0]] = elem[1]; | |
return hash; | |
}, {}); | |
var change; | |
// Here is your change, ma'am. | |
var result = cash - price; | |
var total = cid.reduce(function (p, c) { | |
return p + c[1]; | |
}, 0.0).toFixed(2); | |
if (result > total) | |
return "Insufficient Funds"; | |
else if (result.toFixed(2) === total) | |
return "Closed"; | |
change = cid.reverse().reduce(function (p, c) { | |
if (result > cashes[c[0]]) { | |
// 1. get the change needed for this round | |
var needed_change = result - result % cashes[c[0]]; | |
// 2. get the minimal change from needed and maximium of cash available | |
var current_change = needed_change < c[1] ? needed_change : c[1]; | |
// 3. get the rest needed to calculate changes | |
result = Math.round((result - current_change) * 100.0) / 100; | |
// 4. push current change to the out result | |
p.push([c[0], current_change]); | |
console.log(needed_change, current_change, result); | |
return p; | |
} | |
else | |
return p; | |
}, []); | |
console.log(result.toFixed(2), total, change); | |
if (result.toFixed(2) !== '0.00') | |
return "Insufficient Funds"; | |
return change; | |
} | |
// 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]] | |
drawer(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