Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

“Status: INSUFFICIENT_FUNDS”: if cash-in-drawer is less than the change due, or if you cannot return the exact change.
“Status: CLOSED”: if cash-in-drawer is equal to the change due.
“Status: OPEN”: if cash-in-drawer is greater than the change due and you can return change, with the change due in coins and bills sorted in highest to lowest order. — give me an idea for condition

Your code so far

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

/* file: script.js */
const changeDue = document.getElementById("change-due");
const cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");

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

purchaseBtn.addEventListener("click", () => {
  const cashFloat = parseFloat(cash.value);
  if (cashFloat < price) {
    alert("Customer does not have enough money to purchase the item");
  } else if (cashFloat == price) {
    changeDue.innerHTML = `
    <p>No change due - customer paid with exact cash</p>
    `;
  } else {
    let remmaing = cashFloat - price;
    calculate(remmaing);
  }
});
const calculate = (leftMoney) => {
  let totalLeft = leftMoney;
  let cidMoney = [];
  let cidName = [];
  let drawerIndex = [];

  let cidAmount = [
  ['PENNY', 0.01],
  ['NICKEL', 0.05],
  ['DIME', 0.1],
  ['QUARTER', 0.25],
  ['ONE', 1],
  ['FIVE', 5],
  ['TEN', 10],
  ['TWENTY', 20],
  ['ONE HUNDRED', 100]
  ];
  for (let i = cidAmount.length - 1; i >= 0; i--) {
    if (leftMoney >= cidAmount[i][1]) {
      let remmaing;
      let needed = Math.floor(leftMoney / cidAmount[i][1]);
      let neededFinal = needed * cidAmount[i][1];
      if (neededFinal >= cid[i][1]) {
        remmaing = (leftMoney - cid[i][1]).toFixed(2);
        leftMoney = remmaing;
        if (cid[i][1] !== 0) {
          cidMoney.push(cid[i][1]);
          cidName.push(cid[i][0]);
          drawerIndex.push(i);
        }
      } else {
        remmaing = (leftMoney - neededFinal).toFixed(2);
        leftMoney = remmaing;
        if (neededFinal !== 0) {
          cidName.push(cid[i][0]);
          cidMoney.push(neededFinal);
          drawerIndex.push(i);
        }
      }
    }
  }

  let totalcid = cid.flat(1).filter(item => typeof item === "number").reduce((acc, el) => acc + el, 0).toFixed(2);
  
  changeFn(cidName, cidMoney, totalcid, totalLeft);
};

const changeFn = (name, money, totalcid, totalLeft) => {
   if (parseFloat(totalcid) === parseFloat(totalLeft) || price < cash.value) {
    changeDue.textContent = "Status: CLOSED";
  } else if (parseFloat(totalLeft) > 0) {
    changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
  } else {
    changeDue.textContent = "Status: OPEN";
  }
  console.log(parseFloat(totalcid));
  console.log(parseFloat(totalLeft));
  for (let i = 0; i <= name.length - 1; i++) {
    changeDue.textContent += ` ${name[i]}: $${money[i]}`;
  }
};
/* file: styles.css */

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Can you say more about what debugging you have tried and how you’re stuck figuring out what’s happening?

I am not getting idea about condition, this statments are confusing me

this is an example of that situation

When price is 19.5, the value in the #cash element is 20, cid is [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]], and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: OPEN QUARTER: $0.5"

you have money left in the drawer, so after transaction the register stay open

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