Cash Register | Coderz Repository

Cash Register

Last updated:

0 purchases

Cash Register Image
Cash Register Images

$4.00

Languages

Categories

Add to Cart

Description:

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:

  • price: A number representing the cost of the item being purchased.
  • cash: A number representing the amount of cash given by the customer.
  • cid: An array of arrays, where each inner array contains a string (the denomination) and a number (the amount of that denomination available in the register).

Function Logic:

  1. Unit Set Definition: The function defines a constant 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).
  2. Total Cash in Drawer Calculation: It calculates the total cash available in the drawer (totalCid) by summing up the amounts from the cid array.
  3. Change Calculation: The function calculates the change that needs to be issued (changeToIssue) by subtracting the price from the cash given.
  4. Insufficient Funds Check:
    • If the changeToIssue is greater than totalCid, it returns an object indicating "INSUFFICIENT_FUNDS".
    • If the changeToIssue exactly matches totalCid, it returns "CLOSED" along with the entire cash-in-drawer.
  5. Change Distribution: If there is sufficient cash in the drawer, the function reverses the 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.
  6. Final Checks:
    • If, after attempting to dispense change, there is still change left to be issued, it returns "INSUFFICIENT_FUNDS".
    • If all conditions are satisfied, it returns "OPEN" along with the change that was successfully dispensed.

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.

Conclusion

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.

Features:

  1. Unit Set Definition: The function defines a constant 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).
  2. Total Cash in Drawer Calculation: It calculates the total cash available in the drawer (totalCid) by summing up the amounts from the cid array.
  3. Change Calculation: The function calculates the change that needs to be issued (changeToIssue) by subtracting the price from the cash given.
  4. Insufficient Funds Check:
    • If the changeToIssue is greater than totalCid, it returns an object indicating "INSUFFICIENT_FUNDS".
    • If the changeToIssue exactly matches totalCid, it returns "CLOSED" along with the entire cash-in-drawer.
  5. Change Distribution: If there is sufficient cash in the drawer, the function reverses the 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.
  6. Final Checks:
    • If, after attempting to dispense change, there is still change left to be issued, it returns "INSUFFICIENT_FUNDS".
    • If all conditions are satisfied, it returns "OPEN" along with the change that was successfully dispensed.

Requirements:

  • Javascript
  • Node.js

Instructions:

Run Javascript File in CLI

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product: (if this is empty don't purchase this product)

Customer Reviews

There are no reviews.