<p dir="auto">Problem: Cash Register
<p dir="auto">Problem Definition: The objective of the the program is to accept three inputs which is the purchase price, the amount paid and the array of currency denominations with their amount i.e cash in drawer array. This was tricky at first but I got my way around it alas! The program will return user change in the form of an array.
<p dir="auto">Algorithm
<ol>
<li><p dir="auto">I created a function which takes three parameters namely purchase price(price), amount tendered(cash) and cid(cash in drawer array).
<li><p dir="auto">I created a variable change which is the difference between the cash and the price.
<li><p dir="auto">A variable called totalCid is created to find the total amount of the cash in the cid array
<li><p dir="auto">A denominations array was declared consisting of various denominations in the expected outcomes.
<li><p dir="auto">The totalCid value was compared against the denominations array.
<li><p dir="auto">The program returns closed if the cid (cash in drawer) is equal to the change, returns insufficient funds if the caash in drawer is less than the change or gives the change value as an array of denominations if the cash in drawer can cater for the user change(cid is greater than change).
<p dir="auto">function checkCashRegister(price, cash, cid) {<br />
var denominations = [<br />
{<br />
denomination: "ONE HUNDRED",<br />
value: 100.00<br />
}, {<br />
denomination: "TWENTY",<br />
value: 20.00<br />
}, {<br />
denomination: "TEN",<br />
value: 10.00<br />
}, {<br />
denomination: "FIVE",<br />
value: 5.00<br />
}, {<br />
denomination: "ONE",<br />
value: 1.00<br />
}, {<br />
denomination: "QUARTER",<br />
value: 0.25<br />
}, {<br />
denomination: "DIME",<br />
value: 0.10<br />
}, {<br />
denomination: "NICKEL",<br />
value: 0.05<br />
}, {<br />
denomination: "PENNY",<br />
value: 0.01<br />
}<br />
];<br />
var change = cash - price;<br />
var totalCid = cid.reduce(function(accumulator, next) {
<pre><code> return accumulator + next[1];
}, 0.0);
if (totalCid === change) {
return "Closed";
}
cid = cid.reverse();
var result = denominations.reduce(function(accumulator, next, index) {
var currentValue = 0.0;
if (change >= next.value) {
while (change >= next.value && cid[index][1] >= next.value) {
currentValue += next.value;
change -= next.value;
change = Math.round(change * 100) / 100;
cid[index][1] -= next.value;
}
accumulator.push([next.denomination, currentValue]);
return accumulator;
} else {
return accumulator;
}
}, []);
return result.length && change === 0
? result
: "Insufficient Funds";
<p dir="auto">}<br />
// Example cash-in-drawer array:<br />
// [["PENNY", 1.01],<br />
// ["NICKEL", 2.05],<br />
// ["DIME", 3.10],<br />
// ["QUARTER", 4.25],<br />
// ["ONE", 90.00],<br />
// ["FIVE", 55.00],<br />
// ["TEN", 20.00],<br />
// ["TWENTY", 60.00],<br />
// ["ONE HUNDRED", 100.00]]
<p dir="auto">checkCashRegister(19.50, 20.00, [<br />
[<br />
"PENNY", 1.01<br />
],<br />
[<br />
"NICKEL", 2.05<br />
],<br />
[<br />
"DIME", 3.10<br />
],<br />
[<br />
"QUARTER", 4.25<br />
],<br />
[<br />
"ONE", 90.00<br />
],<br />
[<br />
"FIVE", 55.00<br />
],<br />
[<br />
"TEN", 20.00<br />
],<br />
[<br />
"TWENTY", 60.00<br />
],<br />
["ONE HUNDRED", 100.00]<br />
]);
A hard challange, good job!