{"id":5382,"date":"2021-09-27T09:18:28","date_gmt":"2021-09-27T08:18:28","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=5382"},"modified":"2022-07-25T08:35:41","modified_gmt":"2022-07-25T07:35:41","slug":"javascript-cash-register","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-cash-register\/","title":{"rendered":"Javascript Cash Register"},"content":{"rendered":"\n<p>Source: <a href=\"https:\/\/stackoverflow.com\/questions\/38831446\/calculating-exact-change-with-javascript\">https:\/\/stackoverflow.com\/questions\/38831446\/calculating-exact-change-with-javascript<\/a><\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"typescript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">function checkCashRegister(price: number, cash: number, cid: Array&lt;Array&lt;any>>) {\n    let change = 100 * (cash - price);\n    const moneyValues = [1, 5, 10, 25, 100, 500, 1000, 2000, 10000];\n    const amtToReturn = [];\n\n    for (let i = cid.length - 1; i >= 0; i--) {\n        let amt = 0;\n        while (moneyValues[i] &lt;= change &amp;&amp; cid[i][1] > 0 &amp;&amp; change > 0) {\n            console.log(`subtracting ${moneyValues[i]}`);\n            cid[i][1] -= moneyValues[i] \/ 100; \/\/ reduce amount in cid\n            change -= moneyValues[i]; \/\/ reduce amount from change\n            amt += moneyValues[i] \/ 100; \/\/ keep track of how much money was taken out of cid\n        }\n        if (amt !== 0) {\n            \/\/ adds record of amount taken out of cid\n            amtToReturn.push([cid[i][0], amt]);\n        }\n    }\n\n    \/\/ if there is still some change left over\n    if (change !== 0) {\n        console.log(change);\n        return \"Insufficient Funds\";\n    }\n\n    \/\/ if there is any money left in cid, it returns amtToReturn\n    for (let j = 0; j &lt; cid.length; j++) {\n        if (cid[j][1] > 0) {\n            return amtToReturn;\n        }\n    }\n\n    \/\/ if register is empty\n    return \"Closed\";\n}\n\n\/\/ Example cash-in-drawer array:\n\/\/ [[\"PENNY\", 1.01], 0\n\/\/ [\"NICKEL\", 2.05], 1\n\/\/ [\"DIME\", 3.10],   2\n\/\/ [\"QUARTER\", 4.25],3\n\/\/ [\"ONE\", 90.00],   4\n\/\/ [\"FIVE\", 55.00],  5\n\/\/ [\"TEN\", 20.00],   6\n\/\/ [\"TWENTY\", 60.00],7\n\/\/ [\"ONE HUNDRED\", 100.00]]8\n\ncheckCashRegister(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]]);<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/stackoverflow.com\/questions\/38831446\/calculating-exact-change-with-javascript<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4,1],"tags":[],"class_list":["post-5382","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=5382"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5382\/revisions"}],"predecessor-version":[{"id":5469,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5382\/revisions\/5469"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=5382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=5382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=5382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}