Tell us what’s happening:
I’m getting several test failures. Really can’t tell why.
From the 7th test on although i test each and they all look good.
My alerts come out with the correct text (tried it with and without “”).
Can someone have a look and help out?
Thanks in advance
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>
<label for="price">Price of Item: </label>
<input type="number" id="price" placeholder="Enter price" step="0.01">
<br>
<label for="cash">Cash Provided: </label>
<input type="number" id="cash" placeholder="Enter cash" step="0.01">
<br>
<button id="purchase-btn">Purchase</button>
<p id="change-due">Change Due</p>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
const priceInput = document.getElementById("price");
const cashInput = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDueElement = 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],
];
const currencyUnits = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100,
};
const checkCashRegister = (price, cash, cid) => {
const changeDue = cash - price;
let totalCid = cid.reduce((sum, denom) => sum + denom[1], 0);
totalCid = parseFloat(totalCid.toFixed(2));
if (cash < price) {
alert("Customer does not have enough money to purchase the item");
updateChangeDue("INSUFFICIENT_FUNDS", []);
return;
}
if (changeDue === 0) {
updateChangeDue("CLOSED", []);
return;
}
if (totalCid < changeDue) {
updateChangeDue("INSUFFICIENT_FUNDS", []);
return;
}
const change = [];
let remainingChange = changeDue;
cid.reverse().forEach(([unit, amount]) => {
if (currencyUnits[unit] <= remainingChange && amount > 0) {
let unitChange = 0;
while (remainingChange >= currencyUnits[unit] && amount > 0) {
remainingChange -= currencyUnits[unit];
remainingChange = parseFloat(remainingChange.toFixed(2));
amount -= currencyUnits[unit];
unitChange += currencyUnits[unit];
}
change.push([unit, parseFloat(unitChange.toFixed(2))]);
}
});
const totalChange = change.reduce((sum, [, value]) => sum + value, 0);
if (totalChange < changeDue || remainingChange > 0) {
updateChangeDue("INSUFFICIENT_FUNDS", []);
return;
}
if (totalChange === totalCid) {
updateChangeDue("CLOSED", change);
return;
}
updateChangeDue("OPEN", change);
};
const updateChangeDue = (status, change) => {
if (status === "INSUFFICIENT_FUNDS") {
changeDueElement.textContent = "Status: INSUFFICIENT_FUNDS";
} else if (status === "CLOSED") {
changeDueElement.textContent = `Status: CLOSED ${change.map(([unit, amount]) => `${unit}: $${amount}`).join(" ")}`;
} else if (status === "OPEN") {
changeDueElement.textContent = `Status: OPEN ${change.map(([unit, amount]) => `${unit}: $${amount}`).join(" ")}`;
} else {
changeDueElement.textContent = "Unexpected status.";
}
};
purchaseBtn.addEventListener("click", () => {
const price = parseFloat(priceInput.value);
const cash = parseFloat(cashInput.value);
if (isNaN(price) || isNaN(cash) || price <= 0 || cash <= 0) {
alert("Please enter valid amounts for price and cash provided");
return;
}
// Case 9 & 10: Exact cash
if (price === cash) {
changeDueElement.textContent = "No change due - customer paid with exact cash";
return;
}
checkCashRegister(price, cash, cid);
});
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register