Tell us what’s happening:
Good Afternoon, i’m unable to achieve step 12 in the certification would anyone be so kind to let me know where i’m going wrong with the code please:
Your code so far
<!-- file: index.html -->
<!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>
<h1>Cash Register App</h1>
<label for="cash">Enter Cash Amount:</label>
<input type="number" id="cash" placeholder="Cash Amount" step="0.01" />
<button id="purchase-btn">Purchase</button>
<p id="change-due"></p>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
let price = 3.26; // Set price to 3.26
let cid = [ // Cash in drawer
['PENNY', 1.01],
['NICKEL', 2.05],
['DIME', 3.10],
['QUARTER', 4.25],
['ONE', 90.00],
['FIVE', 55.00],
['TEN', 20.00],
['TWENTY', 60.00],
['ONE HUNDRED', 100.00]
];
document.getElementById("purchase-btn").addEventListener("click", function() {
let cash = parseFloat(document.getElementById("cash").value);
let changeDue = (cash - price).toFixed(2);
if (cash < price) {
alert("Customer does not have enough money to purchase the item");
return;
}
if (cash === price) {
document.getElementById("change-due").innerText = "No change due - customer paid with exact cash";
return;
}
let totalInDrawer = cid.reduce((total, denom) => total + denom[1], 0).toFixed(2);
if (totalInDrawer < changeDue) {
document.getElementById("change-due").innerText = "Status: INSUFFICIENT_FUNDS";
return;
}
let changeArray = [];
let changeToReturn = parseFloat(changeDue);
let changeStatus = "Status: OPEN";
for (let i = cid.length - 1; i >= 0; i--) {
let coinName = cid[i][0];
let coinTotal = cid[i][1];
let denominationValue = getDenominationValue(coinName);
let amountToReturn = 0;
while (changeToReturn >= denominationValue && coinTotal > 0) {
amountToReturn += denominationValue;
changeToReturn -= denominationValue;
coinTotal -= denominationValue;
changeToReturn = parseFloat(changeToReturn.toFixed(2));
coinTotal = parseFloat(coinTotal.toFixed(2));
}
if (amountToReturn > 0) {
changeArray.push(`${coinName}: $${amountToReturn.toFixed(2)}`);
cid[i][1] = coinTotal;
}
}
if (changeToReturn > 0) {
document.getElementById("change-due").innerText = "Status: INSUFFICIENT_FUNDS";
return;
}
if (cid.every(denom => denom[1] === 0)) {
changeStatus = "Status: CLOSED";
}
let changeOutput = changeArray.length > 0 ? changeArray.join(" ") : "";
document.getElementById("change-due").innerText = changeStatus + (changeOutput ? " " + changeOutput : "");
});
// Function to get the value of each denomination
function getDenominationValue(coinName) {
switch (coinName) {
case 'PENNY': return 0.01;
case 'NICKEL': return 0.05;
case 'DIME': return 0.10;
case 'QUARTER': return 0.25;
case 'ONE': return 1.00;
case 'FIVE': return 5.00;
case 'TEN': return 10.00;
case 'TWENTY': return 20.00;
case 'ONE HUNDRED': return 100.00;
default: return 0;
}
}
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register