05. Dezember 2021

Cash Register

Simuliert ein Kasse. Man legt einen Betrag fest, zahlt einen bestimmten Summe ein und erhält das Wechselgeld.

function checkCashRegister(price, cash, cid) {
  const states = ["INSUFFICIENT_FUNDS", "CLOSED", "OPEN"];
  const units = [
    ["ONE HUNDRED", 100],
    ["TWENTY", 20],
    ["TEN", 10],
    ["FIVE", 5],
    ["ONE", 1],
    ["QUARTER", 0.25],
    ["DIME", 0.1],
    ["NICKEL", 0.05],
    ["PENNY", 0.01],
  ];
  /** Total amount of money in register */
  const cidTotal = Number(parseFloat(cid.reduce((total, current) => total + current[1], 0)).toFixed(2));
  
  /** Holds the change */
  let change = [];
  
  /** Difference between total price and given money */
  let diffInit = cash - price;
  let diff = diffInit;
  
  /** Total amount of change*/
  let diffTotal = 0;

  /** Holds status */
  let status;
  
  /** Reverse the value of the cid param */
  cid = cid.reverse();

  for (let i = 0; i < cid.length; i++) {
    if (cid[i][1] > 0) {
      let amountForUnit;
      if (cid[i][1] > diff) {
        const countsForDiff = Math.floor(diff / units[i][1]);
        amountForUnit = countsForDiff * units[i][1];
        if (countsForDiff > 0) {
          change.push([units[i][0], amountForUnit])
        }
      } else {
        const countsPerCidUnit = cid[i][1] / units[i][1];
        amountForUnit = countsPerCidUnit * units[i][1];
        change.push([units[i][0], amountForUnit])
      }
      diff = Number((diff - amountForUnit).toFixed(2));
      diffTotal = Number((diffTotal + amountForUnit).toFixed(2));
    }
  }

  if (diffInit == diffTotal && cidTotal > diffTotal) {
    status = states[2];
  } else if (diffInit == diffTotal && cidTotal == diffTotal) {
    status = states[1];
    change = [...cid].reverse();
  } else {
    status = states[0];
    change = [];
  }
      
  return {
    status: status,
    change: change
  };
}
 
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

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]]) should return an object.

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]]) should return {status: "OPEN", change: [["QUARTER", 0.5]]}.

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]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}.

freeCodeCamp