Cash Register Project

My code is working perfectly. Everything is working as expected, but there are two failed tests and I don’t know why. I tried everything, but I’m sure that I missed something. This is my Code:

let price = 1.87;  
let cid = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED", 100]
];

const denominationValues = {
  "PENNY": 0.01,
  "NICKEL": 0.05,
  "DIME": 0.1,
  "QUARTER": 0.25,
  "ONE": 1,
  "FIVE": 5,
  "TEN": 10,
  "TWENTY": 20,
  "ONE HUNDRED": 100
};  



cid.sort((a, b) => denominationValues[b[0]] - denominationValues[a[0]]);

let cashAmount = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
let priceText = document.getElementById("price-display");

priceText.textContent = `${price}`;

const statusOne = "Status: INSUFFICIENT_FUNDS";
const statusTwo = "Status: CLOSED";
const statusThree = "Status: OPEN";

let cidTotal = 0;
cid.forEach(item => {
    cidTotal += item[1];
});
cidTotal = Math.round(cidTotal * 100);

let cidListFunction = () => {
    let cidList = document.getElementById("cid-list");
    cidList.innerHTML = "";
    cid.forEach(item => {  
        let listItem = document.createElement("li");
        listItem.textContent = `${item[0]}: $${item[1].toFixed(2)}`;
        cidList.appendChild(listItem);
    });
};
cidListFunction();

//price, cashAmount, cid

const letsPurchase = () => {  
    let rest = Math.round((parseFloat(cashAmount.value) - price) * 100);

    if (parseFloat(cashAmount.value) < price) {
        alert("Customer does not have enough money to purchase the item");
        return;
    } else if (parseFloat(cashAmount.value) === price) {
        changeDue.textContent = "No change due - customer paid with exact cash";  
        return; 
    } else if (cidTotal < rest) {
        changeDue.textContent = `${statusOne}`;
        return;
    }

    // Create a copy of cid to revert changes if needed
    let originalCid = JSON.parse(JSON.stringify(cid));
    let change = [];

    cid.forEach((item, index) => {
        let denomination = item[0];
        let denominationValue = Math.round(denominationValues[denomination] * 100); 
        let amountInDrawer = Math.round(item[1] * 100); 
        let amountToReturn = 0;

        while (rest >= denominationValue && amountInDrawer >= denominationValue) {  
            rest -= denominationValue; 
            amountInDrawer -= denominationValue;
            amountToReturn += denominationValue;
        }

        if (amountToReturn > 0) {
            change.push([denomination, amountToReturn / 100]); 
            cid[index][1] = amountInDrawer / 100;  
        }
    });

    cidListFunction();       

    if (rest > 0) {
        changeDue.textContent = `${statusOne}`;
        // Revert cid to its original state
        cid = originalCid;
        cidListFunction();
        return;  
    } else {
        let cidEmpty = cid.every(item => item[1] === 0);  

        if (cidEmpty) {
            let status = `${statusTwo}: `;
            let changeList = change.map(([denomination, amount]) => `${denomination}: $${amount.toFixed(2)}`).join(" ");
            changeDue.textContent = status + changeList;
            return;
        } else {
            let status = `${statusThree} `;
            let changeList = change.map(([denomination, amount]) => `${denomination}: $${amount.toFixed(2)}`).join(" ");
            changeDue.textContent = status + changeList;
        }
    }
};

purchaseBtn.addEventListener("click", letsPurchase);

how to test: assign a different value to price and cid below your event listener. Then try to use your app. You need to make it work in this situation as this is the situation the tests work in

Thank you for the reply, I try bu it didnt work.

Hi @ceballoscallerosgast

Please post your updated code so the forum can assist.

Happy coding

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