Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Describe your issue in detail here. it’s bugged

Your code so far

<!-- file: index.html -->

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Cash Register</title>
  </head>
  <body>
    <input type="number" id="cash" name="cash" />
    <div id="change-due"></div>
    <button id="purchase-btn">Purchase</button>

    <script src="./script.js"></script>
  </body>
</html>
/* file: styles.css */

/* file: script.js */
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 notes = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];

const cashInput = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");

const makePurchase = () => {
  let cash = Number(cashInput.value);

  if (!cash) {
    changeDue.textContent = "";
    return;
  }

  switch (true) {
    case cash < price:
      alert("Customer does not have enough money to purchase the item");
      break;
    case cash === price:
      changeDue.textContent = "No change due - customer paid with exact cash";
      break;
    default:
      let change = cash * 100 - price * 100;
      let depletedNumberOfNotes = 0;
      let changeDueTextContent = "";

      for (let i = notes.length - 1; i >= 0; i--) {
        if (cid[i][1] === 0) depletedNumberOfNotes++;

        const note = notes[i] * 100;
        const numberOfNotesToReturn = Math.trunc(change / note);
        if (!numberOfNotesToReturn) continue;

        const cashAmountOfNotesInCashDrawer = cid[i][1] * 100;
        if (!cashAmountOfNotesInCashDrawer) continue;

        const cashToReturnFromNote = Math.min(
          numberOfNotesToReturn * note,
          cashAmountOfNotesInCashDrawer
        );

        if (cashToReturnFromNote > 0) {
          const cashToSubstract = cashToReturnFromNote / 100;
          cid[i][1] -= cashToSubstract;
          if (cid[i][1] === 0) depletedNumberOfNotes++;
          changeDueTextContent += `${cid[i][0]}: $${cashToSubstract} `;
          change -= cashToReturnFromNote; // change % note;
        }
      }

      if (change) {
        changeDueTextContent = "Status: INSUFFICIENT_FUNDS";
      } else if (depletedNumberOfNotes === cid.length) {
        changeDueTextContent = "Status: CLOSED " + changeDueTextContent;
        // Status: CLOSED QUARTER: $0 DIME: $0 NICKEL: $0 PENNY: $0.5
      } else {
        changeDueTextContent = "Status: OPEN " + changeDueTextContent;
      }

      changeDue.textContent = changeDueTextContent;
      
  }
};

purchaseBtn.addEventListener("click", makePurchase);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/build-a-cash-register-project/build-a-cash-register The code is bugged, fix it pls, cuz we can’t procced forward

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

There’s an issue here:

The final test doesn’t have the correct message for the change-due output

YEs, but still doesn’t work. Guess it has to be remake!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.