This is my code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cash Register</title>
<meta name="viewport" content="width=device.width,initial-scale=1:0">
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Cash Register</h1>
<div id="change">
<h2>Enter cash from customer:</h2>
<input id="cash" type="number" DOM="" />
<button id="purchase-btn">Confirm</button>
<p id="change-due"></p>
</div>
<div id="cash-register">
<div id="total"><p class="total"></p></div>
<div id="thing"></div>
<div id="register-body">
<div id="buttons">
<div id="button">1</div>
<div id="button">2</div>
<div id="button">3</div>
<div id="button">4</div>
<div id="button">5</div>
<div id="button">6</div>
<div id="button">7</div>
<div id="button">8</div>
<div id="button">0</div>
<div id="button">+</div>
<div id="button">-</div>
<div id="button">*</div>
<div id="button"class="equals">=</div>
</div>
<div id="change-counter">
<p id="units">Available change:</p>
<p id="unit"></p>
<p id="unit"></p>
<p id="unit"></p>
<p id="unit"></p>
<p id="unit"></p>
<p id="unit"></p>
<p id="unit"></p>
<p id="unit"></p>
<p id="unit"></p>
</div>
</div>
<div id="drawer"><div id="knob"></div></div>
</div>
<script src="script.js"></script>
</body>
</html>
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]
];
const total = document.querySelector(".total")
const cidName = cid.map(subArr=>subArr[0])
const cidVal = cid.map(subArr=>subArr[1])
const cash = document.querySelector("#cash");
const btn = document.getElementById("purchase-btn");
const units = document.querySelectorAll("#unit");
const changeDue = document.getElementById("change-due");
cash.addEventListener('input', () => {
});
for (let i=0;i<9;i++){
units[i].textContent += `${cidName[i]}: $${cidVal[i]}`
}
total.textContent = price
const reset = () =>{
changeDue.textContent = ""
}
const getChange = () => {
reset()
let cidRev = cid.reverse()
let changeMap = {}
let cid2 = [...cid]
let denoms = [100,20,10,5,1,0.25,0.10,0.05,0.01]
const totalCid = cid.reduce((acc, curr) => acc + curr[1], 0);
let denomNames = ["ONE HUNDRED","TWENTY","TEN","FIVE","ONE","QUARTER","DIME","NICKEL","PENNY"]
let totalChange = Number(cash.value) - price
if (price > Number(cash.value)) {
alert("Customer does not have enough money to purchase the item")
} else if (price === Number(cash.value)) {
if (totalChange === totalCid) {
changeDue.textContent = `Status: CLOSED ${cidName[0]}: $${cidVal[0].toFixed(2)}`;
} else {
changeDue.textContent = "No change due - customer paid with exact cash"
}
} else {
if (price > totalCid){
changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
} else {
for (let i = 0; i < denoms.length; i++) {
if (totalChange >= denoms[i]) {
let amountGiven = 0;
while (totalChange >= denoms[i] && cid[i][1] > 0) {
totalChange -= denoms[i];
totalChange = Math.round(totalChange * 100) / 100;
cid[i][1] -= denoms[i];
amountGiven += denoms[i];
if (cid[i][1] < denoms[i]) break;
}
if (amountGiven > 0) {
if (changeMap[denomNames[i]]) {
changeMap[denomNames[i]] += amountGiven;
} else {
changeMap[denomNames[i]] = amountGiven;
}
}
}
}
if (totalChange > 0) {
changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
return;
}
let formattedChange = Object.entries(changeMap).map(([key, value]) => `${key}: $${parseFloat(value).toFixed(value % 1 === 0 ? 0 : 2)}`).join(' ');
changeDue.textContent = `Status: OPEN ${formattedChange}`
}
}
}
btn.addEventListener("click",()=>getChange(cash.value))
it passes every test except for tests 18 and 19 on this project: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/build-a-cash-register-project/build-a-cash-register
I’ve been slaving away at this for hours but can’t get to understand why would it not pass, i tried searching the freecodecamp forum and even asked chatgpt for help but im stuck so I thought I’ll just create a post myself. Thanks for help in advance. (my thesis is it’s caused by the fact some of my things have more than 2 decimals but in some places when i put .toFixed(2) it just breaks the code so i’m lost)
EDIT: I probably should add that the main issue is, when cid runs out to all 0’s what happens is “No change due…” goes to work instead of “Status:CLOSED…”