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();
}
})