Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

The last test :
" 1. When price is 19.5, the value in the #cash element is 20, cid is [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]], and the #purchase-btn element is clicked, the value in the #change-due element should be Status: CLOSED QUARTER: $0 DIME: $0 NICKEL: $0 PENNY: $0.5."

Q1 : Why does the request only include “QUARTER” to “PENNY” rather than “ONE HUNDRED” to “PENNY” or only “PENNY” ? It is kind of illogical.

Q2: I can not pass the test. Even if I copied the text “Status: CLOSED QUARTER: $0 DIME: $0 NICKEL: $0 PENNY: $0.5” for test, I failed. I can not find out why it does not work.

Your code so far

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

1 Like

There’s bug in the last test, for details see:

2 Likes

having similar issue, tried many things, what’s the solution?

We don’t just give solutions here, so don’t expect anyone to paste the exact solution in here for you. Instead, we try to help you figure it out for yourself.

If you are still having problems with this step then please use the Help button in the step to create a new post just for your issue.

Thank you, but there’s literally an open github issue on it, did I misread something?

Ahh, you’re right, I was thinking of something else and got it mixed up with this one. For this issue, there is no solution at the moment. You can’t pass the last test until the test itself is fixed.

It seems like this link’s script.js code is the solution, still studying why this works rather than some other code

const purchaseButton = document.getElementById('purchase-btn');
const cashInput = document.getElementById('cash');
const changeDueDiv = document.getElementById('change-due');

let price = 19.5; // let allows fcc tests to reassign
let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]; // Example cash in drawer

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

    if (Number(cashInput.value) === price) {
        changeDueDiv.innerHTML = 'No change due - customer paid with exact cash';
        cashInput.value = '';
        return;
    }

    let changeDue = Number(cashInput.value) - price;
    let change = calculateChange(changeDue, cid);

    if (change.status === "INSUFFICIENT_FUNDS") {
        changeDueDiv.innerHTML = 'Status: INSUFFICIENT_FUNDS';
    } else {
        changeDueDiv.innerHTML = `Status: ${change.status} ${change.change.map(c => `${c[0]}: $${c[1].toFixed(2)}`).join(' ')}`;
    }

    cashInput.value = '';
}

purchaseButton.addEventListener('click', checkCashRegister);



const calculateChange = (changeDue, cid) => {
    const currencyUnit = {
        "ONE HUNDRED": 100,
        "TWENTY": 20,
        "TEN": 10,
        "FIVE": 5,
        "ONE": 1,
        "QUARTER": 0.25,
        "DIME": 0.1,
        "NICKEL": 0.05,
        "PENNY": 0.01
    };

    let totalCID = cid.reduce((sum, elem) => sum + elem[1], 0);
    totalCID = parseFloat(totalCID.toFixed(2));

    if (totalCID < changeDue) {
        return { status: "INSUFFICIENT_FUNDS", change: [] };
    }

    let changeArr = [];
    let reversedCid = [...cid].reverse();

    for (let [denom, amount] of reversedCid) {
        let value = currencyUnit[denom];
        let amountOfDenom = 0;

        while (changeDue >= value && amount > 0) {
            amount -= value;
            changeDue -= value;
            changeDue = parseFloat(changeDue.toFixed(2));
            amountOfDenom += value;
        }

        if (amountOfDenom > 0) {
            changeArr.push([denom, amountOfDenom]);
        }
    }

    if (changeDue > 0) {
        return { status: "INSUFFICIENT_FUNDS", change: [] };
    }

    let changeTotal = changeArr.reduce((sum, elem) => sum + elem[1], 0);
    if (changeTotal < totalCID) {
        return { status: "OPEN", change: changeArr };
    }

    // Specific format for 'CLOSED' status
    return { 
        status: "CLOSED", 
        change: "CLOSED " + Object.keys(currencyUnit)
            .map(denom => {
                let amount = changeArr.find(elem => elem[0] === denom)?.[1] || 0;
                return `${denom}: $${amount.toFixed(2)}`;
            }).join(' ')
    };
};

Yea, I encounter this too and I can’t pass the test. It says it was fixed ( fix(challenge): fix the last test of the Cash Register Project by weilirs · Pull Request #52929 · freeCodeCamp/freeCodeCamp · GitHub )
and it has been already merged into :main ,but I still have bugged instruction on this test.
When changes on github code take effect on site ?

The fixed was just merged yesterday. As I understand it, there is no set timeline as to when it will actually be pushed to production. But I would be surprised if it took more than a week.

Well, it took over a week. It’s still not fixed.

It has been fixed already. The last test request change to Status: CLOSED PENNY: $0.5 .