Tell us what’s happening:
My code is failing tests 18 and 19. I used console.log statements to figure out why, and it’s returning INSUFFICIENT-FUNDS for test 18. I’ve gone over my code but have no idea why. Can someone help me figure this out?
Your code so far
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale = 1.0" />
<meta charset="UTF-8" />
<title>freeCodeCamp Cash Register Project</title>
</head>
<body>
<h1>Cash Register</h1>
<form>
<label for="cash">Enter cash from customer:</label>
<input id="cash" type="text" />
<div id="change-due""></div>
<button id="purchase-btn" type="button">Purchase</button>
</form>
<script src="script.js"></script>
</body>
</html>
let price = 1.87;
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 purchaseBtn = document.getElementById("purchase-btn");
const changeDueDiv = document.getElementById("change-due");
purchaseBtn.addEventListener("click", () => {
const cash = parseFloat(document.getElementById("cash").value);
if (cash < price) {
alert("Customer does not have enough money to purchase the item");
changeDueDiv.innerText = "";
return;
}
let changeDue = calculateChange(cash, price, cid);
if (changeDue === "INSUFFICIENT_FUNDS") {
changeDueDiv.innerText = `Status: ${changeDue}`;
console.log(`Status: ${changeDue}`);
} else if (changeDue.length === 0) {
changeDueDiv.innerText = "No change due - customer paid with exact cash";
console.log("No change due - customer paid with exact cash");
} else if (changeDue.status === "CLOSED") {
changeDueDiv.innerText = `Status: CLOSED, Change due: ${formatChange(changeDue.change)}`;
console.log(`Status: CLOSED, Change due: ${formatChange(changeDue.change)}`);
} else {
changeDueDiv.innerText = `Status: OPEN, Change due: ${formatChange(changeDue.change)}`;
console.log(`Status: OPEN, Change due: ${formatChange(changeDue.change)}`);
}
});
const calculateChange = (cash, price, cid) => {
let totalCid = parseFloat(cid.reduce((sum, curr) => sum + curr[1], 0).toFixed(2));
let change = cash - price;
let changeArr = [];
const currencyUnits = [
{ name: "ONE HUNDRED", value: 100.00 },
{ name: "TWENTY", value: 20.00 },
{ name: "TEN", value: 10.00 },
{ name: "FIVE", value: 5.00 },
{ name: "ONE", value: 1.00 },
{ name: "QUARTER", value: 0.25 },
{ name: "DIME", value: 0.10 },
{ name: "NICKEL", value: 0.05 },
{ name: "PENNY", value: 0.01 }
];
// Handle exact cash (no change needed)
if (cash === price) {
return [];
}
// Handle "CLOSED" status if total cash matches change due
if (totalCid === change) {
let closedChange = cid.map(item => [...item]); // Deep copy of cid
closedChange.sort((a, b) => currencyUnits.findIndex(u => u.name === a[0]) - currencyUnits.findIndex(u => u.name === b[0]));
return { status: "CLOSED", change: closedChange };
}
for (let unit of currencyUnits) {
let amount = 0;
while (change >= unit.value && getUnitAmount(unit.name, cid) > 0) {
let available = getUnitAmount(unit.name, cid);
let toTake = Math.min(available, Math.floor(change / unit.value) * unit.value); // Take multiples of unit.value
change -= toTake;
change = Math.round(change * 100) / 100;
amount += toTake;
updateCid(unit.name, cid, toTake);
}
if (amount > 0) {
changeArr.push([unit.name, amount]);
}
}
if (change > 0) {
return "INSUFFICIENT_FUNDS";
} else {
// Proper sorting
changeArr.sort((a, b) => currencyUnits.findIndex(u => u.name === a[0]) - currencyUnits.findIndex(u => u.name === b[0]));
return { status: "OPEN", change: changeArr };
}
};
const getUnitAmount = (unit, cid) => {
for (let item of cid) {
if (item[0] === unit) {
return item[1];
}
}
return 0;
};
const updateCid = (unit, cid, value) => {
for (let item of cid) {
if (item[0] === unit) {
item[1] -= value;
break;
}
}
};
const formatChange = (changeArr) => {
return changeArr.map(item => `${item[0]}: $${item[1].toFixed(2)}`).join(', ');
};
No CSS yet
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register