Tell us what’s happening:
i have been unable to pass step 16-19 i really dont know what i’m doing wrong, i’m also having this error with the nickle when i’m returning change in the change due the nickle will have lots of decimal points i tried using toFixed but its not working, i was also hoping the innerContent.innerText will decrement with the cid when item is purchased but its not doing that i need help. 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">
<title>Cash Register</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<header>
<h1 class="title">Cash Register Project</h1>
</header>
<main>
<div id="change-due" class="change-due"></div>
<label for="cash">Enter cash from customer:</label>
<input id="cash" class="cash" type="number" /><br />
<button id="purchase-btn" class="purchase-btn" type="button"><strong>Purchase</strong></button>
<div class="border">
<div id="total" class="total"></div>
</div>
<div class="connector" ></div>
<div class="cid-container">
<div id="cid-content" class="inner-container"></div>
</div>
<div class="rect">
<div class="ball"></div>
</div>
</main>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
const changeDue = document.getElementById("change-due");
const input = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const cashInDrawer = document.querySelectorAll("cid");
const innerContent = document.getElementById("cid-content");
const total = document.getElementById("total");
let price = 3.26;
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]
];
let currencyUnit = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100
}
total.innerHTML = `<h2>Total: $${price}</h2>`;
innerContent.innerHTML = `<h3>Change in Drawer:</h3>
<div class="cid-content">Pennies: $${cid[0][1]}</div>
<div class="cid-content">Nickels: $${cid[1][1]}</div>
<div class="cid-content">Dimes: $${cid[2][1]}</div>
<div class="cid-content">Quarters: $${cid[3][1]}</div>
<div class="cid-content">Ones: $${cid[4][1]}</div>
<div class="cid-content">Fives: $${cid[5][1]}</div>
<div class="cid-content">Tens: $${cid[6][1]}</div>
<div class="cid-content">Twenties: $${cid[7][1]}</div>
<div class="cid-content">Hundreds: $${cid[8][1]}</div>`;
const calculateChange = (price, cash, cid) => {
const totalCid = Number(cid.reduce((sum, el) => sum + el[1], 0).toFixed(2));
let change = Number((cash - price).toFixed(2));
if (totalCid < change) {
changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
} else if (totalCid === change && price < cash) {
changeDue.innerText = "Status: CLOSED " + cid;
} else {
let changeArr = [];
cid.reverse().forEach((el) => {
let coinName = el[0];
let totalAmountInDrawer = Number(el[1]);
let equivalentOfAmountInDrawer = Number(currencyUnit[coinName]);
let availableAmount = Number((totalAmountInDrawer / equivalentOfAmountInDrawer).toFixed(2));
let amountCollected = 0;
while (change >= equivalentOfAmountInDrawer && availableAmount > 0) {
change = Number((change - equivalentOfAmountInDrawer).toFixed(2));
availableAmount--;
amountCollected++;
}
if (amountCollected > 0) {
changeArr.push(` ${coinName}:` + `$${amountCollected * equivalentOfAmountInDrawer}`);
}
});
changeDue.innerText = "Status: OPEN " + changeArr;
}
}
purchaseBtn.addEventListener("click", () => {
let userInput = parseFloat(input.value);
if (userInput < price) {
alert("Customer does not have enough money to purchase the item");
input.value = "";
return;
} else if (userInput === price) {
changeDue.innerHTML = "No change due - customer paid with exact cash";
input.value = "";
return "";
} else {
calculateChange(price, userInput, cid);
input.value = "";
}
})
/* 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 Edg/136.0.0.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register