Skip to content

Instantly share code, notes, and snippets.

@MunimIftikhar
Last active November 11, 2022 10:37
Show Gist options
  • Select an option

  • Save MunimIftikhar/c2bc32c154e0b517c1d0b98b6e438a3b to your computer and use it in GitHub Desktop.

Select an option

Save MunimIftikhar/c2bc32c154e0b517c1d0b98b6e438a3b to your computer and use it in GitHub Desktop.
freeCodeCamp: Cash Register

Problem statement

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.

Input example #1

price = 19.5
cash = 20
cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]

Expected output #1

{status: "OPEN", change: [["QUARTER", 0.5]]}

Input example #2

price = 3.26
cash = 100
cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]

Expected output #2

{status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}

Solution code

let coins = [
    ["ONE HUNDRED", 100],
    ["TWENTY", 20],
    ["TEN", 10],
    ["FIVE", 5],
    ["ONE", 1],
    ["QUARTER", 0.25],
    ["DIME", 0.1],
    ["NICKEL", 0.05],
    ["PENNY", 0.01],  
  ];

function sum(arr) {
  let result = 0;
  for (let i = 0; i < arr.length; i++) {
    result += arr[i][1];
  }
  return result;
}

function checkCashRegister(price, cash, cid) {
  let priceDifference = cash - price;
  let cidTotalAmount = sum(cid);
  let change = [];

  if (cidTotalAmount < priceDifference) {
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }
  else if (cidTotalAmount === priceDifference) {
    return {status: "CLOSED", change: cid};
  }   
  else {
    cid = cid.reverse();
    let currChange = 0;
    let i = 0;
    for (i = 0; i < coins.length; i++) {
      while (priceDifference>0 && priceDifference >= coins[i][1] && currChange < cid[i][1] ) {
        priceDifference -= coins[i][1];
        priceDifference = priceDifference.toFixed(2)
        currChange += coins[i][1];
      }
      if (currChange > 0) {
        change.push([coins[i][0], currChange]);
      }
      currChange = 0; 
    } 
  if (priceDifference != 0) {
      return {status: "INSUFFICIENT_FUNDS", change: []};
    }
  }

  return {status: "OPEN", change: change}
}

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]]));

Problem link

freeCodeCamp Cash Register

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment