Tell us what’s happening:
Hi, I’ve been trying to complete this for two weeks now and I’m stuck on the final two steps. It states that if price is 19.5 and my input is 20 it should be OPEN Penny, my output is OPEN quarter which makes sense to me. I’m also having trouble regarding the last step involving the CLOSE status. I did ask AI to help debug and it couldn’t figure it out either so I’m not sure if it’s a bug. Thanks!
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">
<link rel="stylesheet" href="styles.css">
<title>Cash Register</title>
</head>
<body>
<h1>Cash Register</h1>
<input id="cash" />
<button id="purchase-btn">Sale</button>
<div id="change-due"></div>
<span id="change-due"></span>
<p id="change-due"></p>
<script src="./script.js"></script>
</body>
</html>
/* file: script.js */
// Global Variables
const cash = document.getElementById("cash");
const saleBtn = document.getElementById("purchase-btn");
const change = document.getElementById("change-due");
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]
];
let price = 19.5;
const currencyUnits = [
['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.10],
['QUARTER', 0.25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE HUNDRED', 100]
];
saleBtn.addEventListener("click", () => {
let cashValue = parseFloat(cash.value);
let changeDue = parseFloat((cashValue - price).toFixed(2));
if (cashValue < price) {
alert("Customer does not have enough money to purchase the item");
return;
}
if (cashValue === price) {
change.innerText = "No change due - customer paid with exact cash";
return;
}
const result = getChange(changeDue, cid);
if (result.status === "INSUFFICIENT_FUNDS") {
change.innerText = `Status: ${result.status}`;
} else if (result.status === "CLOSED") {
change.innerText = `Status: ${result.status} ${formatChange(result.change)}`;
} else {
change.innerText = `Status: ${result.status} ${formatChange(result.change)}`;
}
});
const getChange = (changeDue, cid) => {
let totalCid = parseFloat(cid.reduce((sum, [_, amount]) => sum + amount, 0).toFixed(2));
const originalCid = JSON.parse(JSON.stringify(cid)); // Deep copy to preserve original drawer
let changeArray = [];
let remainingChange = changeDue;
for (let i = currencyUnits.length - 1; i >= 0; i--) {
const [unit, unitValue] = currencyUnits[i];
let amountAvailable = cid[i][1];
let amountToGive = 0;
while (remainingChange >= unitValue && amountAvailable >= unitValue) {
remainingChange = parseFloat((remainingChange - unitValue).toFixed(2));
amountAvailable = parseFloat((amountAvailable - unitValue).toFixed(2));
amountToGive = parseFloat((amountToGive + unitValue).toFixed(2));
}
if (amountToGive > 0) {
changeArray.push([unit, amountToGive]);
}
}
// If we cannot make the exact change
if (remainingChange > 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
// Calculate total change given
const totalChangeGiven = parseFloat(changeArray.reduce((sum, [_, amount]) => sum + amount, 0).toFixed(2));
if (totalChangeGiven === totalCid) {
return { status: "CLOSED", change: originalCid };
}
return { status: "OPEN", change: changeArray };
};
const formatChange = (changeArray) =>
changeArray.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).join(" ");
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15
Challenge Information:
Build a Cash Register Project - Build a Cash Register