Tell us what’s happening:
I have this one last test failing
When price is less than the value in the #cash element, total cash in drawer cid is equal to change due, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: CLOSED” with change due in coins and bills sorted in highest to lowest order.
it seems my code says status opened when the cid = the change due but i dont know why
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<HTML>
<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 Project</title>
</head>
<body>
<input id="cash">
<button id="purchase-btn">Purchase</button>
<div id="change-due"></div>
<script src="script.js"></script>
</body>
</HTML>
/* file: script.js */
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 cash = document.getElementById("cash");
const purchase = document.getElementById("purchase-btn");
const change = document.getElementById("change-due");
let currencyUnits = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.10],
["QUARTER", 0.25],
["ONE", 1.00],
["FIVE", 5.00],
["TEN", 10.00],
["TWENTY", 20.00],
["ONE HUNDRED", 100.00]
];
purchase.addEventListener("click", () => {
const cashValue = parseFloat(cash.value);
const changeDue = cashValue - price;
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 changeResult = getChange(changeDue, cid);
if (changeResult.status === "INSUFFICIENT_FUNDS" || changeResult.status === "CLOSED") {
change.innerText = `Status: ${changeResult.status}`;
if (changeResult.change.length > 0) {
change.innerText += ` - Change: ${formatChange(changeResult.change)}`;
}
} else {
let changeText = `Status: OPEN - Change: ${formatChange(changeResult.change)}`;
change.innerText = changeText;
}
});
const getChange = (changeDue, cid) => {
let totalCid = parseFloat(cid.reduce((sum, [_, amount]) => sum + amount, 0).toFixed(2));
let changeArr = [];
let remainingChange = changeDue;
for (let i = currencyUnits.length - 1; i >= 0; i--) {
let unit = currencyUnits[i][0];
let unitValue = currencyUnits[i][1];
let unitInDrawer = cid[i][1];
if (unitValue <= remainingChange && unitInDrawer > 0) {
let amountFromUnit = 0;
while (remainingChange >= unitValue && unitInDrawer > 0) {
remainingChange = parseFloat((remainingChange - unitValue).toFixed(2));
unitInDrawer -= unitValue;
amountFromUnit += unitValue;
}
if (amountFromUnit > 0) {
changeArr.push([unit, parseFloat(amountFromUnit.toFixed(2))]);
}
}
}
if (remainingChange > 0) {
return {status: "INSUFFICIENT_FUNDS", change: []};
}
if (changeDue === totalCid) {
return {status: "CLOSED", change: changeArr};
}
return {status: "OPEN", change: changeArr};
};
const formatChange = changeArr => changeArr.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).join(", ");
/* 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/127.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register