JavaScript Cash Register
Requirements
- Implement a cash register drawer function that accepts a purchase price, cash payment amount, and "cash-in-drawer". Returns an object in the following format containing a "status" string and a "change" array:
-
{status: "INSUFFICIENT_FUNDS", change: []}
if the funds in "cash-in-drawer" are insufficent, or don't have the correct currencies for the exact change. The "change" array is empty. -
{status: "CLOSED", change: [...]}
if the funds in "cash-in-drawer" are equal to the required change. The "change" array is equal to the "cash-in-drawer" array, sorted in lowest to highest currency value. -
{status: "OPEN", change: [...]}
if the funds in "cash-in-drawer" are greater than the required change. The "change" array denotes the currencies for the required change in a similar format to the "cash-in-drawer" array, except sorted in highest to lowest currency value. - Example "cash-in-drawer" array:
[["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]
Implementation
checkCashRegister(price, cash, cid)
calculates the required change for a purchase. price
is the purchase price, cash
is the cash payment amount, cid
is the "cash-in-drawer" array. Returns an object containing "status" string and "change" array.
Example Usage
Use the browser console to interact with the function (Ctrl + Shift + I
, then select the "Console" tab):
> 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]]);
// Returns:
// { status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]] }