Tell us what’s happening:
I dont understand why the last test is asking for the output to be “Status: CLOSED PENNY: $0.5” when the change for 19.5 when paying with $20 should be .50 in quarters?
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cash Register</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Cash Register</h1>
<label for="cash">Customer Cash:</label>
<input type="number" id="cash" min="0" step="any" placeholder="Enter cash amount">
<button id="purchase-btn">Purchase</button>
<div id="change-due">Change Due</div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
body {
font-family: Arial, sans-serif;
padding: 20px;
}
h1 {
color: #333;
}
#cash {
margin-right: 10px;
}
#purchase-btn {
background-color: #28a745;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
#purchase-btn:hover {
background-color: #218838;
}
#change-due {
margin-top: 20px;
font-size: 1.2em;
color: #555;
}
/* file: script.js */
let price = 19.5;
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]
];
document.getElementById("purchase-btn").addEventListener("click", function() {
const cash = parseFloat(document.getElementById("cash").value);
const change = cash - price;
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;
}
function calculateChange(change, cid) {
const currency = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100
};
let changeDue = [];
let remainingChange = change;
const sortedCid = cid.sort((a, b) => currency[b[0]] - currency[a[0]]);
for (let [unit, amount] of sortedCid) {
if (remainingChange > 0) {
let unitValue = currency[unit];
let unitsRequired = Math.floor(remainingChange / unitValue);
let unitsProvided = Math.min(unitsRequired, amount / unitValue);
if (unitsProvided > 0) {
changeDue.push([unit, unitsProvided * unitValue]);
remainingChange -= unitsProvided * unitValue;
remainingChange = Math.round(remainingChange * 100) / 100;
}
}
}
if (remainingChange > 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
const isDrawerEmpty = sortedCid.every(([_, amount]) => amount === 0);
return {
status: isDrawerEmpty ? "CLOSED" : "OPEN",
change: changeDue
};
}
const result = calculateChange(change, cid);
let output = `Status: ${result.status}`;
if (result.change.length > 0) {
output += " " + result.change.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).join(" ");
}
document.getElementById("change-due").innerText = output;
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register