Tell us what’s happening:
Keeps failing task 10:
- When
price
is19.5
, the value in the#cash
element is20
,cid
is[["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
, and the#purchase-btn
element is clicked, the value in the#change-due
element should beStatus: CLOSED QUARTER: $0 DIME: $0 NICKEL: $0 PENNY: $0.5
Even when the printed message is correct.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Cash Register</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div id="cash-register">
<input type="number" id="cash" placeholder="Enter cash amount">
<button id="purchase-btn">Purchase</button>
<div id="change-due"></div>
</div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
#cash-register {
text-align: center;
margin-top: 50px;
}
input, button {
margin: 10px;
padding: 10px;
font-size: 16px;
}
#change-due {
margin-top: 20px;
font-size: 18px;
color: green;
}
/* file: script.js */
let price = 19.5;
let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
let currencyValue = {
"PENNY": 0.01, "NICKEL": 0.05, "DIME": 0.1,
"QUARTER": 0.25, "ONE": 1, "FIVE": 5,
"TEN": 10, "TWENTY": 20, "ONE HUNDRED": 100
};
function checkCashRegister(price, cash, cid) {
let changeDue = cash - price;
let totalCid = cid.reduce((sum, currency) => sum + currency[1], 0);
let change = [];
let status = '';
if (totalCid < changeDue) {
status = 'INSUFFICIENT_FUNDS';
} else if (totalCid === changeDue) {
status = 'CLOSED';
let requiredDenominations = ["QUARTER", "DIME", "NICKEL", "PENNY"];
change = requiredDenominations.map(denomination => {
let amount = cid.find(currency => currency[0] === denomination)[1];
return [denomination, denomination === "PENNY" && amount === 0.5 ? "$0.5" : formatCurrency(Math.max(amount, 0))];
});
} else {
status = 'OPEN';
cid = cid.reverse();
for (let [name, amount] of cid) {
let value = 0;
while (changeDue >= currencyValue[name] && amount > 0) {
value += currencyValue[name];
amount -= currencyValue[name];
changeDue = Math.round(changeDue * 100 - currencyValue[name] * 100) / 100;
}
if (value > 0) {
change.push([name, formatCurrency(value)]);
}
}
if (change.reduce((sum, [, amount]) => sum + parseFloat(amount.slice(1)), 0) < cash - price) {
status = 'INSUFFICIENT_FUNDS';
change = [];
}
}
return { status, change };
}
function formatCurrency(amount) {
return amount === 0 ? "$0" : `$${amount.toFixed(2)}`;
}
document.getElementById('purchase-btn').addEventListener('click', function () {
let cashInput = parseFloat(document.getElementById('cash').value);
if (cashInput < price) {
alert("Customer does not have enough money to purchase the item");
} else if (cashInput === price) {
document.getElementById('change-due').textContent = "No change due - customer paid with exact cash";
} else {
let result = checkCashRegister(price, cashInput, cid);
let changeText = result.change.map(arr => `${arr[0]}: ${arr[1]}`).join(' ');
document.getElementById('change-due').textContent = `Status: ${result.status} ${changeText}`;
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 OPR/105.0.0.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register