Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ASSIGNMENT
- _______________________________________________________________________________________________________________________________
- Design 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) as the third argument.
- cid is a 2D array listing available currency.
- The checkCashRegister() function should always return an object with a status key and a change key.
- Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due,
- or if you cannot return the exact change.
- Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
- Otherwise, 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.
- _______________________________________________________________________________________________________________________________
- SOLUTION
- _______________________________________________________________________________________________________________________________
- function checkCashRegister(price, cash, cid) {
- var result = {status: "", change: []};
- // // Here is your change, ma'am.
- var change = cash - price;
- var cashValue = [
- { name: "ONE HUNDRED", value: 100 },
- { name: "TWENTY", value: 20 },
- { name: "TEN", value: 10 },
- { name: "FIVE", value: 5 },
- { name: "ONE", value: 1 },
- { name: "QUARTER", value: 0.25 },
- { name: "DIME", value: 0.1 },
- { name: "NICKEL", value: 0.05 },
- { name: "PENNY", value: 0.01 }
- ];
- var cashInDrowerSum = cid.reduce(function(acc, item) { // get sum of all money we have in our drawer ('cid')
- return acc + item[1];
- }, 0);
- if (cashInDrowerSum < change) {
- result.status = "INSUFFICIENT_FUNDS";
- return result;
- }
- if (cashInDrowerSum === change) {
- result.status = "CLOSED";
- result.change = cid;
- return result;
- }
- cid = cid.reverse(); // we are provided with an array ('cid'), where currency units of larger amount come last.
- // We need to reverse this array, so that they come in highest to lowest order
- var reduced = cashValue.reduce(function(acc, next, index) { // we use optional parameter 'index' to get the index (position) of an
- // element from 'cashValue' array. Then we use this index to get access
- // to the correspondent element from the previously reversed 'cid' array
- // (e.g., first element from 'cashValue' array is an
- // object { name: "Hundred", value: 100 }. Its index is 0.
- // It corresponds to the first element from the 'cid' array,
- // ["ONE HUNDRED", 100]).
- var currentValue = 0;
- if (change >= next.value) {
- while (change >= next.value && cid[index][1] >= next.value) {
- currentValue += next.value; //add next.value-s, until it equals to 'change'
- change -= next.value; // decrement 'change' by the 'next.value' (which we added to the 'currentValue')
- change = Math.round(change * 100) / 100;
- cid[index][1] -= next.value; // take 'next.value' from our drawer
- }
- acc.push([next.name, currentValue]); // we provided 'initial value' (see line 83) and set it to an empty array,
- // so 'acc' is equal to an empty array.
- // That is, var reduced = [].push([next.name, currentValue])
- return acc;
- } else {
- return acc;
- }
- }, []); // we need to provide 2-dimentional array to pass the test,
- // so we need to specify an empty array, as our initial value
- if (reduced.length < 1 || change > 0) { // if there are no elements in our 'reduced' array,
- // or if we still owe a change...
- // for example, if we have to give 50 cents,
- // but have only one dollar bill and one penny in our cash in drawer
- // ( checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0],
- // ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0],
- // ["TWENTY", 0], ["ONE HUNDRED", 0]]) )
- result.status = "INSUFFICIENT_FUNDS";
- return result;
- }
- result.status = "OPEN";
- result.change = reduced;
- return result;
- }
- 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]])
Add Comment
Please, Sign In to add comment