Build a Cash Register

please assist to check where the error could be in this code.I have redone it severally and it works well but three of the tests keeps failing.here is my code;

let cash = document.getElementById("cash");
let changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const totalCidDisplay = document.getElementById("total-cid");

// Define the price
let price = 19.5;

// Define the cash-in-drawer array
let cid = [
    { name: "PENNY", amount: 1.01 },
    { name: "NICKEL", amount: 2.05 },
    { name: "DIME", amount: 3.1 },
    { name: "QUARTER", amount: 4.25 },
    { name: "ONE", amount: 90 },
    { name: "FIVE", amount: 55 },
    { name: "TEN", amount: 20 },
    { name: "TWENTY", amount: 60 },
    { name: "ONE HUNDRED", amount: 100 }
];

// Function to calculate the change
function calculateChange() {
    let cashAmount = parseFloat(cash.value);
    let changeAmount = cashAmount - price;

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

    let change = [];
    let remainingChange = changeAmount;

    for (let i = cid.length - 1; i >= 0; i--) {
        const denomination = cid[i];
        const denominationValue = getDenomUnit(denomination.name);
        let numOfDenomination = 0;

        while (denominationValue <= remainingChange && denominationValue <= denomination.amount) {
            remainingChange -= denominationValue;
            denomination.amount -= denominationValue;
            numOfDenomination++;
            remainingChange = parseFloat(remainingChange.toFixed(2));
        }

        if (numOfDenomination > 0) {
            change.push({ name: denomination.name, amount: numOfDenomination });
        }

        if (remainingChange === 0) {
            break;
        }
    }

    if (remainingChange !== 0) {
        changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
    } else {
        let changeString = "Status: OPEN ";
        for (let denom of change) {
            let denomAmount = denom.amount * getDenomUnit(denom.name);
            if (denomAmount % 1 !== 0) {
                denomAmount = denomAmount.toFixed(2).replace(/\.?0+$/, ''); // Replace trailing zeroes
            }
            changeString += denom.name + ": $" + denomAmount + " ";
        }
        changeDue.textContent = changeString.trim();
    }

    // Display total cash in drawer after calculation
    totalCidDisplay.textContent = "Total Cash in Drawer: " + getTotalCashInDrawer(cid).toFixed(2);
}

// Function to calculate total cash in drawer
function getTotalCashInDrawer(cid) {
    let total = 0;
    for (let denomination of cid) {
        total += denomination.amount;
    }
    return total;
}

// Helper function to get denomination value
const getDenomUnit = (denomName) => {
    switch (denomName) {
        case "PENNY":
            return 0.01;
        case "NICKEL":
            return 0.05;
        case "DIME":
            return 0.1;
        case "QUARTER":
            return 0.25;
        case "ONE":
            return 1;
        case "FIVE":
            return 5;
        case "TEN":
            return 10;
        case "TWENTY":
            return 20;
        case "ONE HUNDRED":
            return 100;
        default:
            return 0;
    }
};

// Event listener for button click
purchaseBtn.addEventListener("click", calculateChange);

The order in your for loop seems unusual to me, also if you could put all your code into one box and a link to challenge or project may be helpful.

Hello,
Your for loop works quite well; it takes the biggest denomination first and facilitates the payment process. However, your while loop looks like a lengthy process to reduce your denomination.amount. You can make it easier just by comparing how much needs to be paid and how much cash you have, without using a loop. Also, you rounded your remainingChange with two decimal places, which is good, but have you checked how your initial changeAmount calculation looks? Maybe it needs extra attention too :thinking:

Also, please share your code using ‘formatted text’, which can you find in text editor button like this </> or by using the ctrl+e shortcut. And explain what is wrong there, what errors you encountered, how you approached them, and why you couldn’t fix them. When you start to explain, you will better understand the origin of your issue, and we can also better understand your issue :slightly_smiling_face:.
Happy coding.

1 Like

i have adjusted the codes but still these tests does not pass yet when i test them manually they give the given answers.here are the errors.
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"

. When

price

is

3.26

, the value in the

#cash

element is

100

,

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 TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04"

. 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 PENNY: $0.5"

.

Please use ‘preformatted text’ you can use it with three backticks or ctrl+e shortcut or </> button which shown on text editor.

Also please send your ‘adjusted’ code and explain the error, explain what is wrong there, what errors you encountered, how you approached them, and why you couldn’t fix them. Right now, no one can understand what the problem is and how did you change your code.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

please share your html too, it’s needed for debugging