|
| 1 | +--- |
| 2 | +id: 66c58d2477d7622ddc30ae06 |
| 3 | +title: Build a Cash Register |
| 4 | +challengeType: 14 |
| 5 | +dashedName: build-a-cash-register |
| 6 | +--- |
| 7 | + |
| 8 | +# --description-- |
| 9 | + |
| 10 | +**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab. |
| 11 | + |
| 12 | +**User Stories:** |
| 13 | + |
| 14 | +1. You should create 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`, which is is a 2D array listing available currency) as the third argument. |
| 15 | +1. The `checkCashRegister()` function should always return an object with a `status` key and a `change` key. |
| 16 | +1. If cash-in-drawer is less than the change due, or if you cannot return the exact change, the function should return `{status: "INSUFFICIENT_FUNDS", change: []}`. |
| 17 | +1. If the cash-in-drawer is equal to the change due, the function should return `{status: "CLOSED", change: [...]}` with cash-in-drawer as the value for the key `change`. |
| 18 | +1. Otherwise, the function should return `{status: "OPEN", change: [...]}`, with the change due in coins and bills, sorted in highest to lowest order, as the value of the `change` key. |
| 19 | + |
| 20 | +<table><tbody><tr><th>Currency Unit</th><th>Amount</th></tr><tr><td>Penny</td><td>$0.01 (PENNY)</td></tr><tr><td>Nickel</td><td>$0.05 (NICKEL)</td></tr><tr><td>Dime</td><td>$0.1 (DIME)</td></tr><tr><td>Quarter</td><td>$0.25 (QUARTER)</td></tr><tr><td>Dollar</td><td>$1 (ONE)</td></tr><tr><td>Five Dollars</td><td>$5 (FIVE)</td></tr><tr><td>Ten Dollars</td><td>$10 (TEN)</td></tr><tr><td>Twenty Dollars</td><td>$20 (TWENTY)</td></tr><tr><td>One-hundred Dollars</td><td>$100 (ONE HUNDRED)</td></tr></tbody></table> |
| 21 | + |
| 22 | +See below for an example of a cash-in-drawer array: |
| 23 | + |
| 24 | +```js |
| 25 | +[ |
| 26 | + ["PENNY", 1.01], |
| 27 | + ["NICKEL", 2.05], |
| 28 | + ["DIME", 3.1], |
| 29 | + ["QUARTER", 4.25], |
| 30 | + ["ONE", 90], |
| 31 | + ["FIVE", 55], |
| 32 | + ["TEN", 20], |
| 33 | + ["TWENTY", 60], |
| 34 | + ["ONE HUNDRED", 100] |
| 35 | +] |
| 36 | +``` |
| 37 | + |
| 38 | +# --hints-- |
| 39 | + |
| 40 | +`checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])` should return an object. |
| 41 | + |
| 42 | +```js |
| 43 | +assert.deepEqual( |
| 44 | + Object.prototype.toString.call( |
| 45 | + checkCashRegister(19.5, 20, [ |
| 46 | + ['PENNY', 1.01], |
| 47 | + ['NICKEL', 2.05], |
| 48 | + ['DIME', 3.1], |
| 49 | + ['QUARTER', 4.25], |
| 50 | + ['ONE', 90], |
| 51 | + ['FIVE', 55], |
| 52 | + ['TEN', 20], |
| 53 | + ['TWENTY', 60], |
| 54 | + ['ONE HUNDRED', 100] |
| 55 | + ]) |
| 56 | + ), |
| 57 | + '[object Object]' |
| 58 | +); |
| 59 | +``` |
| 60 | + |
| 61 | +`checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])` should return `{status: "OPEN", change: [["QUARTER", 0.5]]}`. |
| 62 | + |
| 63 | +```js |
| 64 | +assert.deepEqual( |
| 65 | + checkCashRegister(19.5, 20, [ |
| 66 | + ['PENNY', 1.01], |
| 67 | + ['NICKEL', 2.05], |
| 68 | + ['DIME', 3.1], |
| 69 | + ['QUARTER', 4.25], |
| 70 | + ['ONE', 90], |
| 71 | + ['FIVE', 55], |
| 72 | + ['TEN', 20], |
| 73 | + ['TWENTY', 60], |
| 74 | + ['ONE HUNDRED', 100] |
| 75 | + ]), |
| 76 | + { status: 'OPEN', change: [['QUARTER', 0.5]] } |
| 77 | +); |
| 78 | +``` |
| 79 | + |
| 80 | +`checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])` should return `{status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}`. |
| 81 | + |
| 82 | +```js |
| 83 | +assert.deepEqual( |
| 84 | + checkCashRegister(3.26, 100, [ |
| 85 | + ['PENNY', 1.01], |
| 86 | + ['NICKEL', 2.05], |
| 87 | + ['DIME', 3.1], |
| 88 | + ['QUARTER', 4.25], |
| 89 | + ['ONE', 90], |
| 90 | + ['FIVE', 55], |
| 91 | + ['TEN', 20], |
| 92 | + ['TWENTY', 60], |
| 93 | + ['ONE HUNDRED', 100] |
| 94 | + ]), |
| 95 | + { |
| 96 | + status: 'OPEN', |
| 97 | + change: [ |
| 98 | + ['TWENTY', 60], |
| 99 | + ['TEN', 20], |
| 100 | + ['FIVE', 15], |
| 101 | + ['ONE', 1], |
| 102 | + ['QUARTER', 0.5], |
| 103 | + ['DIME', 0.2], |
| 104 | + ['PENNY', 0.04] |
| 105 | + ] |
| 106 | + } |
| 107 | +); |
| 108 | +``` |
| 109 | + |
| 110 | +`checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])` should return `{status: "INSUFFICIENT_FUNDS", change: []}`. |
| 111 | + |
| 112 | +```js |
| 113 | +assert.deepEqual( |
| 114 | + checkCashRegister(19.5, 20, [ |
| 115 | + ['PENNY', 0.01], |
| 116 | + ['NICKEL', 0], |
| 117 | + ['DIME', 0], |
| 118 | + ['QUARTER', 0], |
| 119 | + ['ONE', 0], |
| 120 | + ['FIVE', 0], |
| 121 | + ['TEN', 0], |
| 122 | + ['TWENTY', 0], |
| 123 | + ['ONE HUNDRED', 0] |
| 124 | + ]), |
| 125 | + { status: 'INSUFFICIENT_FUNDS', change: [] } |
| 126 | +); |
| 127 | +``` |
| 128 | + |
| 129 | +`checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])` should return `{status: "INSUFFICIENT_FUNDS", change: []}`. |
| 130 | + |
| 131 | +```js |
| 132 | +assert.deepEqual( |
| 133 | + checkCashRegister(19.5, 20, [ |
| 134 | + ['PENNY', 0.01], |
| 135 | + ['NICKEL', 0], |
| 136 | + ['DIME', 0], |
| 137 | + ['QUARTER', 0], |
| 138 | + ['ONE', 1], |
| 139 | + ['FIVE', 0], |
| 140 | + ['TEN', 0], |
| 141 | + ['TWENTY', 0], |
| 142 | + ['ONE HUNDRED', 0] |
| 143 | + ]), |
| 144 | + { status: 'INSUFFICIENT_FUNDS', change: [] } |
| 145 | +); |
| 146 | +``` |
| 147 | + |
| 148 | +`checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])` should return `{status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}`. |
| 149 | + |
| 150 | +```js |
| 151 | +assert.deepEqual( |
| 152 | + checkCashRegister(19.5, 20, [ |
| 153 | + ['PENNY', 0.5], |
| 154 | + ['NICKEL', 0], |
| 155 | + ['DIME', 0], |
| 156 | + ['QUARTER', 0], |
| 157 | + ['ONE', 0], |
| 158 | + ['FIVE', 0], |
| 159 | + ['TEN', 0], |
| 160 | + ['TWENTY', 0], |
| 161 | + ['ONE HUNDRED', 0] |
| 162 | + ]), |
| 163 | + { |
| 164 | + status: 'CLOSED', |
| 165 | + change: [ |
| 166 | + ['PENNY', 0.5], |
| 167 | + ['NICKEL', 0], |
| 168 | + ['DIME', 0], |
| 169 | + ['QUARTER', 0], |
| 170 | + ['ONE', 0], |
| 171 | + ['FIVE', 0], |
| 172 | + ['TEN', 0], |
| 173 | + ['TWENTY', 0], |
| 174 | + ['ONE HUNDRED', 0] |
| 175 | + ] |
| 176 | + } |
| 177 | +); |
| 178 | +``` |
| 179 | + |
| 180 | +# --seed-- |
| 181 | + |
| 182 | +## --seed-contents-- |
| 183 | + |
| 184 | +```js |
| 185 | + |
| 186 | +``` |
| 187 | + |
| 188 | +# --solutions-- |
| 189 | + |
| 190 | +```js |
| 191 | +const denom = [ |
| 192 | + { name: "ONE HUNDRED", val: 100 }, |
| 193 | + { name: "TWENTY", val: 20 }, |
| 194 | + { name: "TEN", val: 10 }, |
| 195 | + { name: "FIVE", val: 5 }, |
| 196 | + { name: "ONE", val: 1 }, |
| 197 | + { name: "QUARTER", val: 0.25 }, |
| 198 | + { name: "DIME", val: 0.1 }, |
| 199 | + { name: "NICKEL", val: 0.05 }, |
| 200 | + { name: "PENNY", val: 0.01 }, |
| 201 | + ]; |
| 202 | + |
| 203 | + function checkCashRegister(price, cash, cid) { |
| 204 | + const output = { status: null, change: [] }; |
| 205 | + let change = cash - price; |
| 206 | + const register = cid.reduce( |
| 207 | + function (acc, curr) { |
| 208 | + acc.total += curr[1]; |
| 209 | + acc[curr[0]] = curr[1]; |
| 210 | + return acc; |
| 211 | + }, |
| 212 | + { total: 0 } |
| 213 | + ); |
| 214 | + if (register.total === change) { |
| 215 | + output.status = "CLOSED"; |
| 216 | + output.change = cid; |
| 217 | + return output; |
| 218 | + } |
| 219 | + if (register.total < change) { |
| 220 | + output.status = "INSUFFICIENT_FUNDS"; |
| 221 | + return output; |
| 222 | + } |
| 223 | + const change_arr = denom.reduce(function (acc, curr) { |
| 224 | + let value = 0; |
| 225 | + while (register[curr.name] > 0 && change >= curr.val) { |
| 226 | + change -= curr.val; |
| 227 | + register[curr.name] -= curr.val; |
| 228 | + value += curr.val; |
| 229 | + change = Math.round(change * 100) / 100; |
| 230 | + } |
| 231 | + if (value > 0) { |
| 232 | + acc.push([curr.name, value]); |
| 233 | + } |
| 234 | + return acc; |
| 235 | + }, []); |
| 236 | + if (change_arr.length < 1 || change > 0) { |
| 237 | + output.status = "INSUFFICIENT_FUNDS"; |
| 238 | + return output; |
| 239 | + } |
| 240 | + output.status = "OPEN"; |
| 241 | + output.change = change_arr; |
| 242 | + return output; |
| 243 | + } |
| 244 | +``` |
0 commit comments