Build a Cash Register Project

Anyone wanna help me out? Stuck here

<!DOCTYPE html>
<html>
    <head>
        <title>Cash Register</title>
    </head>
    <body>
        <div id="change-due"></div>
        <input type="number" id="cash" placeholder="Enter cash from customer">
        <button id="purchase-btn">Purchase</button>
        <div id="change-in-drawer"></div>
        <script src="script.js"></script>
    </body>
</html>
const cash = document.querySelector('#cash');
const changeDue = document.querySelector('#change-due');
const purchaseBtn = document.querySelector('#purchase-btn');
const changeInDrawer = document.querySelector('#change-in-drawer');

let price = 3.26;
const modPrice = price * 100;
let modCash = 0;
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]
];
//*totalCid in a function or not?
let totalCid;
function calculateTotalCid() {
    totalCid = 0;
    for (let i = 0; i < cid.length; i++) {
        totalCid += cid[i][1] * 100;
    }
}

let prevCid;

function copyCid() {
    prevCid = [[],[],[],[],[],[],[],[],[]];
    let i = 0;
    while (i < 9) {
        prevCid[i][0] = cid[i][0];
        prevCid[i][1] = cid[i][1];
        ++i;
    }
}

copyCid();
displayCid();

//*calculate ChangeDue
function calculateTotalChangeDue() {
    calculateTotalCid();
    console.log(modCash, modPrice);
    const totalChangeDue = modCash - modPrice;
    //check cash availability
    if (totalChangeDue < 0) {
        alert('Customer does not have enough money to purchase the item');
        return;
    } else if (totalChangeDue === 0) {
        changeDue.innerHTML = 'No change due - customer paid with exact cash';
        return;
    }
    console.log('total Change Due: ' + totalChangeDue / 100);
    console.log('total CID: ' + totalCid);

    if (totalCid === totalChangeDue) {
        changeDue.innerHTML = 'Status: CLOSED';
        calculateChangeDue(totalChangeDue);
        displayChangeDue();
    } else if (totalCid > totalChangeDue && calculateChangeDue(totalChangeDue) === 0) {
        changeDue.innerHTML = 'Status: OPEN';
        displayChangeDue();
    } else {
        changeDue.innerHTML = 'Status: INSUFFICIENT_FUNDS';
    }
}

//*if the cid < changeDue || cannot return exact exchange -> Status: INSUFFICIANT_FUNDS
//*if cid === changeDue -> Status: CLOSED
//*if cid > changeDue && can return the change -> Status: OPEN

function calculateChangeDue(totalChangeDue) {
    while (totalChangeDue) {
        if (totalChangeDue >= 10000 && cid[8][1]) {
            totalChangeDue -= 10000;
            cid[8][1] -= 100; 
        } else if (totalChangeDue >= 2000 && cid[7][1]) {
            totalChangeDue -= 2000;
            cid[7][1] -= 20;
        } else if (totalChangeDue >= 1000 && cid[6][1]) {
            totalChangeDue -= 1000;
            cid[6][1] -= 10;
        } else if (totalChangeDue >= 500 && cid[5][1]) {
            totalChangeDue -= 500;
            cid[5][1] -= 5;
        } else if (totalChangeDue >= 100 && cid[4][1]) {
            totalChangeDue -= 100;
            cid[4][1] -= 1;
        } else if (totalChangeDue >= 25 && cid[3][1]) {
            totalChangeDue -= 25;
            cid[3][1] = (((cid[3][1] * 100) - 25) / 100).toFixed(2);
        } else if (totalChangeDue >= 10 && cid[2][1]) {
            totalChangeDue -= 10;
            cid[2][1] = (((cid[2][1] * 100) - 10) / 100).toFixed(2);
        } else if (totalChangeDue >= 5 && cid[1][1]) {
            totalChangeDue -= 5;
            cid[1][1] = (((cid[1][1] * 100) - 5) / 100).toFixed(2);
        } else if (totalChangeDue >= 1 && cid[0][1]) {
            totalChangeDue -= 1;
            cid[0][1] = (((cid[0][1] * 100) - 1) / 100).toFixed(2);
        } else {
            for (let i = 0; i < cid.length; i++) {
                cid[i][1] = prevCid[i][1];
            }
            return totalChangeDue;
        }
    }

    return totalChangeDue;
}

//display changeDue & cid
function displayChangeDue() {
    for (let i = 0; i < cid.length; i++) {
        if (cid[i][1] !== prevCid[i][1]) {
            //I am calculating change in bills and displaying them
            const differenceInBills = (((prevCid[i][1] * 100) - (cid[i][1] * 100)) / 100 ).toFixed(2);
            changeDue.innerHTML += `<p>${cid[i][0]}: $${differenceInBills}</p>`; 
        }
    }
    console.log(prevCid);
    console.log(cid);
    displayCid();
}

function displayCid() {
    changeInDrawer.innerHTML = `<p><strong>Change in drawer:</strong></p>`;
    for (let i = 0; i < cid.length; i++) {
        changeInDrawer.innerHTML += `<p>${cid[i][0]}: $${cid[i][1]}</p>`;
    }
}


//*controls
//   |
//   |
//   |
purchaseBtn.addEventListener('click', () => {
    modCash = cash.value * 100;
    calculateTotalChangeDue();
    copyCid();
})

cash.addEventListener('keydown', (event) => {
    if (event.key === 'Enter') {
        modCash = cash.value * 100;
        calculateTotalChangeDue();
        copyCid();
    }
})

Could you be more specific? What is giving you a problem? What have you tried so far?

well the code seems to be working alright but it is still not able to pass the test for eg. Test no. 7

If you want to manually test cases, the modification of price (and cid if that’s needed) should go at the end of code.

So for example below current code should be added:

price = 20;

That more accurately imitates the way tests are handled.

Still the same issue! But thanx anyway🙂

You mean tests are still not passing, or when reproducing failing test, the way I described it still gives correct result?

Test were not passing but when I gave input box an empty string after every execution then it worked for some reason. But still some tests are failing

Then some other changes are still needed. Sum of the returned change on the screen is 96.74, what makes it look like change was calculated from $100.

I did enter 100 in the input box to check the 12th test. What to change?

I don’t know what’s your current code.

Try adding logging to console with the values of the cid, price and #cash element, and whatever other variables you are using in calculations, at the start of callback function handling click event of the button. Check if these values are making sense, when running all tests.

I fixed my code. But still 4 tests are remaining to pass which actually should pass. Thank you so much for the effort. I have practiced and learned JS by doing projects here which is what I wanted mainly. I can the cert from anywhere.

BTW if u do wanna look at my code then we can connect on discord to discuss?!

feel free to join the freeCodeCamp discord server if you prefer Discord to the forum

if you have 4 tests that should pass but don’t there is still some work to be done

I tried everything and the code is working meaning it is producing the required result but still not passing.

I’m sorry, but my crystal ball is not working today, I can’t see your code from over here, nor guess what’s going wrong