Cash Register App - Unable to pass tests

So, I have a code for the Cash Register app; it seems to give the correct output, but the tests are not passing. I have no idea why. The first few tests pass, and the rest fail, even though the output matches what is required. I am lost as to what might be the problem.

const cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const cashDisplay = document.getElementById("cash-inner-display");
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 changeDue = document.getElementById("change-due");
const priceDisplay = document.getElementById("price-display");
let price = 3.26;
let changeUsed = {};
let status = false;
let cidNew = [...cid];


priceDisplay.textContent = `Total: $${price}`;

const formatChangeDrawer = (arr) => {

    arr.forEach(function(element, index) {

        switch (element[0]) {
            case 'PENNY':

                element[0] = 'Pennies';

                break;
            case 'NICKEL':
                element[0] = 'Nickels';
                break;
            case 'DIME':
                element[0] = 'Dimes';
                break;
            case 'QUARTER':
                element[0] = 'Quaters';
                break;
            case 'ONE':
                element[0] = 'Ones';
                break;
            case 'FIVE':
                element[0] = 'Fives';
                break;
            case 'TEN':
                element[0] = 'Tens';
                break;
            case 'TWENTY':
                element[0] = 'Twenties';
                break;
            case 'ONE HUNDRED':
                element[0] = 'Hundreds';
                break;

            default:
                break;
        }
    });

}


const displayCashDrawer = () => {

    formatChangeDrawer(cidNew);

    cashDisplay.innerHTML = `<div class="drawer-title"><strong>Change in drawer:</strong></div>
`

    cashDisplay.innerHTML += cid.map((item) => {

        return `<p class="cash-Items">${item.join(": $")}</p>`;

    }).join("");

}

const clearCashDrawer = () => {

    cashDisplay.innerHTML = "";

}


const priceCheck = () => {

    let cashValue = parseFloat(cash.value);
    let totalCid = cidNew.reduce((item, acc) => item + acc[1], 0);
    let floatPrice = price;

    if (cashValue) {


        if (cashValue === floatPrice) {

            changeDue.textContent = "No change due - customer paid with exact cash";

        } else if (cashValue < floatPrice) {


            alert("Customer does not have enough money to purchase the item");


        } else if (cashValue - floatPrice > totalCid) {

            changeDue.textContent = "Status: INSUFFICIENT_FUNDS";

        } else {

            calculateChange(cidNew);

        }

    } else {

        alert("Enter an amount");

    }

}



















const formatChangeResult = (arr) => {

    arr.forEach(function(element, index) {

        switch (element[0]) {
            case '0.01':

                element[0] = 'PENNY';

                break;
            case '0.05':
                element[0] = 'NICKEL';
                break;
            case '0.1':
                element[0] = 'DIME';
                break;
            case '0.25':
                element[0] = 'QUARTER';
                break;
            case '1':
                element[0] = 'ONE';
                break;
            case '5':
                element[0] = 'FIVE';
                break;
            case '10':
                element[0] = 'TEN';
                break;
            case '20':
                element[0] = 'TWENTY';
                break;
            case '100':
                element[0] = 'ONE HUNDRED';
                break;

            default:
                break;
        }
    });


}













const displayChange = () => {



    let changeDueArr = Object.entries(changeUsed).sort((a, b) => parseFloat(b[0]).toFixed(2) - parseFloat(a[0]).toFixed(2));
    formatChangeResult(changeDueArr);


    changeDue.innerHTML = status ? `<div class="cash-Items">Status: OPEN</div>` : `<div class="cash-Items">Status: CLOSED</div>`

    changeDue.innerHTML += changeDueArr.map((item) => {

        return `<p> ${item.join(": $")}</p>`;

    }).join("");

}











const calculateChange = (arr) => {

    let change = (cash.value - price).toFixed(2);
    let totalCid = parseFloat(arr.reduce((item, acc) => item + acc[1], 0));


    if (change === totalCid) {

        status = false;

    } else if (change < totalCid) {


        status = true;

    }


    while (change != 0) {

        if (change >= 100.00 && arr[8][1] != 0) {

            change = parseFloat(change - 100.00).toFixed(2);
            arr[8][1] = parseFloat(arr[8][1] - 100.00).toFixed(2);

            if (changeUsed[100]) {

                changeUsed[100] = parseFloat(changeUsed[100] + 100);

            } else {

                changeUsed[100] = 100;

            }

        } else if (change >= 20.00 && arr[7][1] != 0) {

            change = parseFloat(change - 20.00).toFixed(2);
            arr[7][1] = parseFloat(arr[7][1] - 20.00).toFixed(2);

            if (changeUsed[20] != undefined) {

                changeUsed[20] = parseFloat(changeUsed[20] + 20);


            } else {

                changeUsed[20] = 20;

            }


        } else if (change >= 10.00 && arr[6][1] != 0) {

            change = parseFloat(change - 10.00).toFixed(2);
            arr[6][1] = parseFloat(arr[6][1] - 10.00).toFixed(2);

            if (changeUsed[10]) {

                changeUsed[10] = parseFloat(changeUsed[10] + 10);


            } else {

                changeUsed[10] = 10;

            }



        } else if (change >= 5.00 && arr[5][1] != 0) {

            change = parseFloat(change - 5.00).toFixed(2);
            arr[5][1] = parseFloat(arr[5][1] - 5.00).toFixed(2);

            if (changeUsed[5]) {

                changeUsed[5] = parseFloat(changeUsed[5] + 5);

            } else {

                changeUsed[5] = 5;

            }


        } else if (change >= 1.00 && arr[4][1] != 0) {

            change = parseFloat(change - 1.00).toFixed(2);
            arr[4][1] = parseFloat(arr[4][1] - 1.00).toFixed(2);

            if (changeUsed[1]) {

                changeUsed[1] = parseFloat(changeUsed[1] + 1);

            } else {

                changeUsed[1] = 1;

            }

        } else if (change >= 0.25 && arr[3][1] != 0) {

            change = parseFloat(change - 0.25).toFixed(2);
            arr[3][1] = parseFloat(arr[3][1] - 0.25).toFixed(2);

            if (changeUsed[0.25]) {

                changeUsed[0.25] = parseFloat(changeUsed[0.25] + 0.25);

            } else {

                changeUsed[0.25] = 0.25;

            }

        } else if (change >= 0.1 && arr[2][1] != 0) {

            change = parseFloat(change - 0.10).toFixed(2);
            arr[2][1] = parseFloat(arr[2][1] - 0.10).toFixed(2);

            if (changeUsed[0.1]) {

                changeUsed[0.1] = parseFloat(changeUsed[0.1] + 0.1);


            } else {

                changeUsed[0.1] = 0.1;

            }


        } else if (change >= 0.05 && arr[1][1] != 0) {

            change = parseFloat(change - 0.05).toFixed(2);
            arr[1][1] = parseFloat(arr[1][1] - 0.05).toFixed(2);

            if (changeUsed[0.05]) {

                changeUsed[0.05] = parseFloat(changeUsed[0.05] + 0.05);


            } else {

                changeUsed[0.05] = 0.05;

            }


        } else if (change >= 0.01 && arr[0][1] != 0) {


            change = parseFloat(change - 0.01).toFixed(2);
            arr[0][1] = parseFloat(arr[0][1] - 0.01).toFixed(2);

            if (changeUsed[0.01]) {

                changeUsed[0.01] = parseFloat(changeUsed[0.01] + 0.01);

            } else {

                changeUsed[0.01] = 0.01;

            }
        }
    }


    clearCashDrawer();
    displayCashDrawer();
    displayChange();


}

displayCashDrawer();



purchaseBtn.addEventListener('click', priceCheck);

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