So I have been struggeling with this project for quite a while now. Can someone please tell me where my problem is with the Status: Closed part, I cant seem to find the solution. I have redo the project 3 times now and trying to find something different even trying to use an AI tool to see if that helps with the problems but it just caused even more. Can someone maybe help me with this?
let price = 19.5;
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 cash = document.getElementById('cash');
const change = document.getElementById('change-due');
const sale = document.getElementById('purchase-btn');
let currencyUnit = [
['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.1],
['QUARTER', 0.25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE HUNDRED', 100]
];
// Listen for the button click
sale.addEventListener('click', () => {
const cashValue = parseFloat(cash.value);
const changeDue = cashValue - price;
// If customer doesn't have enough money
if (cashValue < price) {
alert('Customer does not have enough money to purchase the item');
return;
}
// If customer paid exactly the price
if (cashValue === price) {
change.innerHTML = 'No change due - customer paid with exact cash';
return;
}
// Otherwise, calculate the change due
const changeResult = getChange(changeDue, cid);
// Display result based on status
if (changeResult.status === 'INSUFFICIENT_FUNDS' || changeResult.status === 'CLOSED') {
change.innerHTML = `Status: ${changeResult.status}<br>${formatChange(changeResult.change)}`;
} else {
change.innerHTML = `Status: OPEN<br>${formatChange(changeResult.change)}`;
}
});
// Function to calculate change
const getChange = (changeDue, cid) => {
// Convert to cents to avoid floating-point issues
let changeDueInCents = Math.round(changeDue * 100);
let totalCidInCents = Math.round(cid.reduce((sum, [_, amount]) => sum + amount * 100, 0));
// If total cash in the drawer is less than the change due, return insufficient funds
if (totalCidInCents < changeDueInCents) {
return {
status: 'INSUFFICIENT_FUNDS',
change: []
};
}
// Prepare to calculate the change
let changedArray = [];
let remainingChange = changeDueInCents;
// Create a copy of the drawer to avoid modifying the original
let drawerCopy = cid.map(([unit, amount]) => [unit, Math.round(amount * 100)]);
// Process denominations in descending order (from highest to lowest)
for (let i = currencyUnit.length - 1; i >= 0; i--) {
let unit = currencyUnit[i][0];
let unitValue = Math.round(currencyUnit[i][1] * 100); // Convert to cents
let unitInDrawer = drawerCopy[i][1];
if (unitValue <= remainingChange && unitInDrawer > 0) {
let amountFromUnit = 0;
while (remainingChange >= unitValue && unitInDrawer >= unitValue) {
remainingChange -= unitValue;
unitInDrawer -= unitValue;
amountFromUnit += unitValue;
}
if (amountFromUnit > 0) {
changedArray.push([unit, amountFromUnit / 100]); // Convert back to dollars
}
}
}
// If we couldn't give exact change
if (remainingChange > 0) {
return {
status: 'INSUFFICIENT_FUNDS',
change: []
};
}
// If the total cash in the drawer equals the change due, return CLOSED
if (totalCidInCents === changeDueInCents) {
return {
status: 'CLOSED',
change: cid
};
}
// Otherwise, return the calculated change
return {
status: 'OPEN',
change: changedArray
};
};
// Format the change into a string for display
const formatChange = changedArray => {
return changedArray.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).join('<br>');
};