<div>
  <button type="button" id="run-btn">Run</button>
  <label for="output">console.log() Output:</label>
  <pre id="output"></pre>
</div>
* {
  margin: 0;
}
div {
  border: 3px solid grey;
  margin: auto;
  padding: 2rem 1rem;
  background-color: #29282e;
  border-radius: 0.3rem;
}
pre {
  display: block;
  position: relative;
  top: 1rem;
  color: #97c389;
  overflow: auto;
  line-height: 1.5rem;
  background-color: #1b2b34;
  padding: 0.5rem;
  border: 2px solid #324149;
}
label {
  color: #fff;
  font-family: consolas;
  display: block;
  margin-top: 1rem;
}
// Cash register function that returns an object with cash drawer 'status' and 'change' properties.

function checkCashRegister(price, cash, cid) {
  const currencyUnits = {
    PENNY: 0.01,
    NICKEL: 0.05,
    DIME: 0.1,
    QUARTER: 0.25,
    ONE: 1,
    FIVE: 5,
    TEN: 10,
    TWENTY: 20,
    "ONE HUNDRED": 100
  };
  let change = cash - price;
  const totalCash = cid.reduce((sum, currencyUnit) => sum + currencyUnit[1], 0);
  const changeDue = [];
  if (totalCash < change) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  } else if (totalCash == change) {
    return { status: "CLOSED", change: cid };
  } else {
    let unitValue;
    let cashUnitQty;
    let cashUnitsNeeded;
    for (let i = cid.length - 1; i >= 0; i--) {
      unitValue = currencyUnits[cid[i][0]];
      cashUnitQty = cid[i][1] / unitValue;
      cashUnitsNeeded = Math.floor(change / unitValue);
      if (change == 0) {
        break;
      }
      if (change < unitValue || cid[i][1] == 0) {
        continue;
      } else if (cashUnitQty > cashUnitsNeeded) {
        let cashUnit = [];
        cashUnit.push(cid[i][0]);
        cashUnit.push(unitValue * cashUnitsNeeded);
        changeDue.push(cashUnit);
        change = parseFloat((change - unitValue * cashUnitsNeeded).toFixed(2));
      } else {
        let cashUnit = [];
        cashUnit.push(cid[i][0]);
        cashUnit.push(unitValue * cashUnitQty);
        changeDue.push(cashUnit);
        change = parseFloat((change - unitValue * cashUnitQty).toFixed(2));
      }
    }
  }
  if (change > 0) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  } else {
    return { status: "OPEN", change: changeDue };
  }
}

// output
const output = document.getElementById("output");
const runBtn = document.getElementById("run-btn");
const testArgs = [
  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]
  ]),
  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]
  ]),
  checkCashRegister(19.5, 20, [
    ["PENNY", 0.01],
    ["NICKEL", 0],
    ["DIME", 0],
    ["QUARTER", 0.5],
    ["ONE", 0],
    ["FIVE", 0],
    ["TEN", 0],
    ["TWENTY", 0],
    ["ONE HUNDRED", 0]
  ]),
  checkCashRegister(19.5, 20, [
    ["PENNY", 0.01],
    ["NICKEL", 0],
    ["DIME", 0],
    ["QUARTER", 0],
    ["ONE", 1],
    ["FIVE", 0],
    ["TEN", 0],
    ["TWENTY", 0],
    ["ONE HUNDRED", 0]
  ]),
  checkCashRegister(19.5, 20, [
    ["PENNY", 0.5],
    ["NICKEL", 0],
    ["DIME", 0],
    ["QUARTER", 0],
    ["ONE", 0],
    ["FIVE", 0],
    ["TEN", 0],
    ["TWENTY", 0],
    ["ONE HUNDRED", 0]
  ])
];
runBtn.addEventListener("click", () => {
  if (!output.textContent) {
    for (const x of testArgs) {
      const val = "> " + JSON.stringify(x) + "\n";
      output.textContent += val;
    }
  }
});

// tests
// console.log(
//   1,
//   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]
//   ])
// );

// console.log(
//   2,
//   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]
//   ])
// );

// console.log(
//   3,
//   checkCashRegister(19.5, 20, [
//     ["PENNY", 0.01],
//     ["NICKEL", 0],
//     ["DIME", 0],
//     ["QUARTER", 0.5],
//     ["ONE", 0],
//     ["FIVE", 0],
//     ["TEN", 0],
//     ["TWENTY", 0],
//     ["ONE HUNDRED", 0]
//   ])
// );

// console.log(
//   4,
//   checkCashRegister(19.5, 20, [
//     ["PENNY", 0.01],
//     ["NICKEL", 0],
//     ["DIME", 0],
//     ["QUARTER", 0],
//     ["ONE", 1],
//     ["FIVE", 0],
//     ["TEN", 0],
//     ["TWENTY", 0],
//     ["ONE HUNDRED", 0]
//   ])
// );

// console.log(
//   5,
//   checkCashRegister(19.5, 20, [
//     ["PENNY", 0.5],
//     ["NICKEL", 0],
//     ["DIME", 0],
//     ["QUARTER", 0],
//     ["ONE", 0],
//     ["FIVE", 0],
//     ["TEN", 0],
//     ["TWENTY", 0],
//     ["ONE HUNDRED", 0]
//   ])
// );
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.