I have looked through the posts of others who have had this same problem, but their solutions did not seem to work for me. As my title states, manually I can pass all of the tests, but when I go to run the official tests, most of them don’t pass.
// Define constants for currency denominations
const CURRENCY_UNITS = [
{ name: "ONE HUNDRED", value: 100.0 },
{ name: "TWENTY", value: 20.0 },
{ name: "TEN", value: 10.0 },
{ name: "FIVE", value: 5.0 },
{ name: "ONE", value: 1.0 },
{ name: "QUARTER", value: 0.25 },
{ name: "DIME", value: 0.1 },
{ name: "NICKEL", value: 0.05 },
{ name: "PENNY", value: 0.01 }
];
// Global variables
let price = 0;
let cid = [];
// Function to calculate change and status
function checkCashRegister(price, cash, cid) {
let changeDue = cash - price;
let totalCid = cid.reduce((sum, [_, amount]) => sum + amount, 0).toFixed(2);
let change = [];
if (changeDue > totalCid) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
if (Number(changeDue.toFixed(2)) === Number(totalCid)) {
return { status: "CLOSED", change: cid.filter(([_, amount]) => amount > 0) };
}
cid = cid.reverse(); // Start with the highest denomination
for (let { name, value } of CURRENCY_UNITS) {
let available = cid.find(unit => unit[0] === name)?.[1] || 0;
let amountUsed = 0;
while (changeDue >= value && available >= value) {
changeDue -= value;
available -= value;
amountUsed += value;
changeDue = Number(changeDue.toFixed(2)); // Avoid precision issues
}
if (amountUsed > 0) {
change.push([name, Number(amountUsed.toFixed(2))]);
}
}
if (changeDue > 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
return { status: "OPEN", change };
}
// Function to handle button click
function handlePurchase() {
const getInputValue = (id) => {
const element = document.getElementById(id);
return element ? parseFloat(element.value.trim() || "0") : 0;
};
price = getInputValue("price");
const cashInput = getInputValue("cash");
cid = [
["PENNY", getInputValue("penny")],
["NICKEL", getInputValue("nickel")],
["DIME", getInputValue("dime")],
["QUARTER", getInputValue("quarter")],
["ONE", getInputValue("one")],
["FIVE", getInputValue("five")],
["TEN", getInputValue("ten")],
["TWENTY", getInputValue("twenty")],
["ONE HUNDRED", getInputValue("one-hundred")]
];
if (isNaN(price) || isNaN(cashInput) || cid.some(([_, amount]) => isNaN(amount))) {
alert("Please enter valid numerical values for all inputs.");
return;
}
if (cashInput < price) {
alert("Customer does not have enough money to purchase the item");
return;
}
if (cashInput === price) {
document.getElementById("change-due").textContent = "No change due - customer paid with exact cash";
return;
}
const result = checkCashRegister(price, cashInput, cid);
const resultDisplay = document.getElementById("change-due");
if (result.status === "INSUFFICIENT_FUNDS") {
resultDisplay.textContent = "Status: INSUFFICIENT_FUNDS";
} else if (result.status === "CLOSED") {
resultDisplay.textContent = `Status: CLOSED ${result.change
.map(([name, amount]) => `${name}: $${amount.toFixed(2)}`)
.join(" ")}`;
} else {
resultDisplay.textContent = `Status: OPEN ${result.change
.map(([name, amount]) => `${name}: $${amount.toFixed(2)}`)
.join(", ")}`;
}
}
// Add event listener to purchase button
const purchaseButton = document.getElementById("purchase-btn");
if (purchaseButton) {
purchaseButton.addEventListener("click", handlePurchase);
}