Tell us what’s happening:
Hi. I can’t understand what is going on with my cash register project code. Upon running the code for tests on freecodecamp, they do not pass. However, if I fill in the values for these tests manually into the code, the code passes for the involved test case. Please advise
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset ="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Cash Register Project</title>
<link rel="stylesheet" href="./styles.css" />
</head>
<body>
<main>
<input id="cash" type="number" />
<p id="change-due"></p>
<button id="purchase-btn">Purchase</button>
</main>
<script src="./script.js" ></script>
</body>
</html>
/* file: script.js */
let price = 19.5;
let denominations = {
'PENNY': 0.01,
'NICKEL': 0.05,
'DIME': 0.1,
'QUARTER': 0.25,
'ONE': 1,
'FIVE': 5,
'TEN': 10,
'TWENTY': 20,
'ONE HUNDRED': 100,
};
let cid = {
'PENNY': 0.01,
'NICKEL': 0,
'DIME': 0,
'QUARTER': 0,
'ONE': 0,
'FIVE': 0,
'TEN': 0,
'TWENTY': 0,
'ONE HUNDRED': 0,
};
let inventory = {
'PENNY': cid["PENNY"] / 0.01,
'NICKEL': Math.round(cid["NICKEL"] / 0.05),
'DIME': cid["DIME"] / 0.1,
'QUARTER': cid["QUARTER"] / 0.25,
'ONE': cid["ONE"],
'FIVE': cid["FIVE"] / 5,
'TEN': cid["TEN"] / 10,
'TWENTY': cid["TWENTY"] / 20,
'ONE HUNDRED': cid["ONE HUNDRED"] / 100,
};
const sortedDenom = Object.entries(denominations).sort((a, b) => b[1] - a[1]);
const sortedCid = Object.entries(cid).sort((a, b) => denominations[b[0]] - denominations[a[0]]).reduce((obj, [key, value]) => {
obj[key] = value;
return obj;
}, {});
const sortedInven = Object.entries(inventory).sort((a, b) => denominations[b[0]] - denominations[a[0]]).reduce((obj, [key, value]) => {
obj[key] = value;
return obj;
}, {});
const customerCash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDetails = document.getElementById("change-due");
purchaseBtn.addEventListener("click", () => {
if (customerCash.value < price) {
alert("Customer does not have enough money to purchase the item");
}
if (customerCash.value == price) {
changeDetails.textContent = "No change due - customer paid with exact cash";
} else if (customerCash.value > price) {
getChange(customerCash.value, sortedDenom, sortedInven)
}
});
const isMoneyLeft = (updatedInven, money, sortedDenom) => {
let remainingTotal = 0;
for (const coin in updatedInven) {
const remainingCount = (sortedInven[coin] || 0) - (money[coin] || 0);
if( remainingCount > 0) {
remainingTotal += remainingCount * sortedDenom[coin];
}
}
return remainingTotal > 0;
};
const getChange = (cash, sortedDenom, sortedInven) => {
let changeDue = cash - price;
const result = {};
let updatedInven = { ...sortedInven };
for (const [coin, value] of sortedDenom) {
const maxAvailable = updatedInven[coin] || 0;
const maxNeeded = Math.floor(changeDue / value);
const count = Math.min(maxAvailable, maxNeeded);
if (count > 0) {
result[coin] = count;
updatedInven[coin] -= count;
changeDue = parseFloat((changeDue - count * value).toFixed(2));
}
};
const content = Object.entries(result).map(([coin, count]) => {
const value = denominations[coin] * count;
return `${coin}: $${value.toFixed(2)}`;
})
.join(' ');
if (changeDue > 0) {
changeDetails.textContent = `Status: INSUFFICIENT_FUNDS`;
} else if (changeDue === 0 && Object.values(updatedInven).every(val => val === 0)) {
changeDetails.textContent = `Status: CLOSED ` + content;
} else {
changeDetails.textContent = `Status: OPEN ` + content;
}
};
/* 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/136.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register