Last updated:
0 purchases
The checkCashRegister
function is designed to determine the appropriate change to be returned to a customer after a cash transaction, while also checking the status of the cash register. It takes three parameters: the price of the item, the cash given by the customer, and the cash-in-drawer (CID) which represents the available denominations in the register.
Parameters:
Function Logic:
UNIT_SET
that maps each denomination to its corresponding value in dollars. This includes coins (PENNY, NICKEL, DIME, QUARTER) and bills (ONE, FIVE, TEN, TWENTY, HUNDRED).totalCid
) by summing up the amounts from the cid
array.changeToIssue
) by subtracting the price from the cash given.changeToIssue
is greater than totalCid
, it returns an object indicating "INSUFFICIENT_FUNDS".changeToIssue
exactly matches totalCid
, it returns "CLOSED" along with the entire cash-in-drawer.cid
array to start dispensing change from the highest denomination. It iterates through each denomination, calculating how much of each can be given as change while updating the remaining change needed.Example Usage:
The function is called with various test cases to demonstrate its functionality. For instance:
javascript
console.log(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]]));
This call checks if the cash register can provide the correct change for a $19.50 purchase when the customer pays with a $20 bill, given the available denominations in the register.
The checkCashRegister
function effectively handles cash transactions by calculating the necessary change, checking the availability of funds in the register, and returning appropriate status messages. This function is a practical example of using arrays and objects in JavaScript to solve real-world problems, making it a relevant topic for job interviews focused on coding and algorithmic thinking.
UNIT_SET
that maps each denomination to its corresponding value in dollars. This includes coins (PENNY, NICKEL, DIME, QUARTER) and bills (ONE, FIVE, TEN, TWENTY, HUNDRED).totalCid
) by summing up the amounts from the cid
array.changeToIssue
) by subtracting the price from the cash given.changeToIssue
is greater than totalCid
, it returns an object indicating "INSUFFICIENT_FUNDS".changeToIssue
exactly matches totalCid
, it returns "CLOSED" along with the entire cash-in-drawer.cid
array to start dispensing change from the highest denomination. It iterates through each denomination, calculating how much of each can be given as change while updating the remaining change needed.Run Javascript File in CLI
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.