I just need any advice on my code I cant seem to pass some of the test cases, 6,7,9 and 10 respectively.
If I change the const denominations to the certain out put it wants I pass al cases but the last one
Any advice will be helpful thank you
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cash Register</title>
</head>
<body>
<input type="number" id="cash" placeholder="Enter cash amount">
<button id="purchase-btn">Purchase</button>
<div id="change-due"></div>
<script src="script.js"></script>
</body>
</html>
const price = 19.5;
const 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 displayChangeDue = document.getElementById("change-due");
const cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const formatResults = (status, change) => {
displayChangeDue.textContent = status;
if (change) {
change.forEach((money) => {
displayChangeDue.innerHTML += `<p>${money[0]}: $${money[1].toFixed(2)}</p>`;
});
}
};
const checkCashRegister = () => {
const cashValue = Number(cash.value);
const changeDue = cashValue - price;
if (cashValue < price) {
alert("Customer does not have enough money to purchase the item");
return;
}
if (cashValue === price) {
displayChangeDue.textContent = "No change due - customer paid with exact cash";
return;
}
let totalCID = 0;
cid.forEach((item) => {
totalCID += item[1];
});
if (totalCID < changeDue) {
displayChangeDue.textContent = "Status: INSUFFICIENT_FUNDS";
return;
}
const change = [];
let remainingChangeDue = changeDue;
const denominations = [
["ONE HUNDRED", 100],
["TWENTY", 20],
["TEN", 10],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["DIME", 0.1],
["NICKEL", 0.05],
["PENNY", 0.01],
];
for (let i = 0; i < denominations.length; i++) {
const currencyName = denominations[i][0];
const currencyValue = denominations[i][1];
const index = cid.findIndex((elem) => elem[0] === currencyName);
const cidValue = cid[index][1];
while (remainingChangeDue >= currencyValue && cidValue > 0) {
remainingChangeDue -= currencyValue;
cid[index][1] -= currencyValue;
change.push([currencyName, currencyValue]);
remainingChangeDue = Math.round(remainingChangeDue * 100) / 100;
}
}
if (remainingChangeDue > 0) {
displayChangeDue.textContent = "Status: INSUFFICIENT_FUNDS";
return;
}
if (changeDue === 0) {
formatResults("Status: CLOSED", [["PENNY", cashValue]]);
return;
}
formatResults("Status: OPEN", change);
};
const checkResults = () => {
checkCashRegister();
};
purchaseBtn.addEventListener("click", checkResults);