Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

The Cash Register Product is still bugged. I confirmed the returned string is constructed exactly how the intructions dictate. I see others are having issues with the same bugged project. If anyone has an idea for how to make this clear the final requirement, I’d greatly appreciate it.

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

<div id="change-due"></div>
<div class="input-div">
  <label for="cash">Enter cash:</label>
  <input type="number" id="cash" class="user-input" value="" />
  <button class="check-btn-styles" id="purchase-btn">Purchase</button>
</div>
<div class="container">
  <div class="top-display-screen-container">
    <p id="price-screen" class="price-screen"></p>
  
  </div>
</div>
</main>
<script>
let price = 19.5;
let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

const displayChangeDue = document.getElementById("change-due");
const cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const priceScreen = document.getElementById("price-screen");
const cashDrawerDisplay = document.getElementById("cash-drawer-display");

const formatResults = (status, change) => {
let htmlString = `Status: ${status}`;

// If the drawer is CLOSED, display all denominations including coins
// If the drawer is CLOSED, display all denominations including coins
if (status === "CLOSED") {
const coinDenominations = ["QUARTER", "DIME", "NICKEL", "PENNY"];

coinDenominations.forEach(denomination => {
  const coinEntry = change.find(entry => entry[0] === denomination);
  const coinValue = coinEntry ? coinEntry[1] : 0;
  htmlString += ` ${denomination}: $${coinValue}`;
});

} else {
  // If status is OPEN or there are bills due, display all denominations
  const currencyDenominations = [
      "ONE HUNDRED", "TWENTY", "TEN", "FIVE", "ONE", "QUARTER", "DIME", "NICKEL", "PENNY"
  ];

  currencyDenominations.forEach(denomination => {
      // Find the corresponding entry in the change array
      const denominationEntry = change.find(entry => entry[0] === denomination);

      // If the entry exists, display it; otherwise, display with a value of 0
      htmlString += `${denomination}: $${denominationEntry ? denominationEntry[1] : 0} `;
  });
}

displayChangeDue.innerHTML = htmlString;
return;
};

const checkCashRegister = () => {
if (Number(cash.value) < price) {
  alert("Customer does not have enough money to purchase the item");
  cash.value = "";
  return;
}

if (Number(cash.value) === price) {
  displayChangeDue.innerHTML =
      "<p>No change due - customer paid with exact cash</p>";
  cash.value = "";
  return;
}

let changeDue = Number(cash.value) - price;
let reversedCid = [...cid].reverse();
let denominations = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
let result = { status: "OPEN", change: [] };
let totalCID = parseFloat(
  cid
  .map((total) => total[1])
  .reduce((prev, curr) => prev + curr)
  .toFixed(2),
);

if (totalCID < changeDue) {
  return (displayChangeDue.innerHTML = "<p>Status: INSUFFICIENT_FUNDS</p>");
}

if (totalCID === changeDue) {
  result.status = "CLOSED";
}

for (let i = 0; i <= reversedCid.length; i++) {
  if (changeDue > denominations[i] && changeDue > 0) {
      let count = 0;
      let total = reversedCid[i][1];
      while (total > 0 && changeDue >= denominations[i]) {
          total -= denominations[i];
          changeDue = parseFloat((changeDue -= denominations[i]).toFixed(2));
          count++;
      }
      if (count > 0) {
          result.change.push([reversedCid[i][0], count * denominations[i]]);
      }
  }
}
if (changeDue > 0) {
  return (displayChangeDue.innerHTML = "<p>Status: INSUFFICIENT_FUNDS</p>");
}

formatResults(result.status, result.change);
};

const checkResults = () => {
if (!cash.value) {
  return;
}
checkCashRegister();
};

purchaseBtn.addEventListener("click", checkResults);

cash.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
  checkResults();
}
});
</script>
</body>
</html>

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

this is fixed in the codebase, at next deployment the changes will be live