Tell us what’s happening:
Grand rising to Everyone;
I have been working on the cash register project for about three days now. During the time frame, I have been trying seriously hard to complete the project on my own, but I have had some difficulties along the way as well. I have manage to setup a code which passed 17 requirements of the test , and two failed(number 14 & 15 respectively). Can someone please provide me with some clue. Your help will be greatly appreciated .
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
let price = 1.87;
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 changeDueElement = document.getElementById("change-due");
const cashInputElement = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const priceElement = document.getElementById("price-screen");
const cashInDrawer = document.getElementById("cash-in-drawer")
const layOut = (status, change) => {
changeDueElement.innerHTML = `<p>Status: ${status}</p>`;
changeDueElement.innerHTML += change.map(([cashId, cashUnit]) =>`<p>${cashId}: $${cashUnit}`).join("");
};
const cashToRegister = () => {
const cashToCent = Math.round(Number(cashInputElement.value) * 100);
const priceToCent = Math.round(price * 100);
if (cashToCent < priceToCent) {
alert("Customer does not have enough money to purchase the item");
cashInputElement.value = "";
return;
}
if (cashToCent === priceToCent) {
changeDueElement.innerHTML =`<p>No change due - customer paid with exact cash</p>`;
cashInputElement.value = "";
return;
}
let returnCash = cashToCent - priceToCent;
const cidReverse = [...cid].reverse().map(([cashId, cashUnit]) =>[
cashId, Math.round(cashUnit * 100)
]);
const cashIds = [10000, 2000, 1000, 500, 100, 25, 10, 5, 1];
const resultObj = {status: "OPEN", change: []};
const sumOfCid = cidReverse.reduce((initialCash, [_, cashUnit]) => initialCash + cashUnit, 0);
if (sumOfCid < returnCash) {
changeDueElement.innerHTML = `<p>INSUFFICIENT_FUNDS</p>`;
return;
}
if (sumOfCid === returnCash) {
resultObj.status = "CLOSED";
}
for (let n = 0; n <= cidReverse.length; n++) {
if (returnCash >= cashIds[n] && returnCash > 0) {
const [cashId, sum] = cidReverse[n];
const handChange = Math.min(sum, returnCash);
const counterCheck = Math.floor(handChange / cashIds[n]);
const sumOfChange = counterCheck * cashIds[n];
returnCash -= sumOfChange;
if (counterCheck > 0) {
resultObj.change.push([cashId, sumOfChange / 100]);
updateCashId(resultObj.change);
}
}
}
if (returnCash > 0) {
changeDueElement.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`;
return;
}
if (cashInputElement.value === price && returnChange < cashInputElement.value) {
changeDueElement.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`;
return;
}
layOut(resultObj.status, resultObj.change);
updateCashId(resultObj.change);
};
const confirmDueOver = () => {
if (!cashInputElement.value) {
return;
}
cashToRegister();
};
const updateCashId = (cashId) => {
const capitalID = {
PENNY: "Pennies",
NICKEL: "Nickels",
DIME: "Dimes",
QUARTER: "Quarters",
ONE: "Ones",
FIVE: "Fives",
TEN: "Tens",
TWENTY: "Twenties",
"ONE HUNDRED": "Hundreds"
}
if (cashId) {
cashId.forEach(([cashCategory, cashValue ]) => {
const aimArray = cid.find(([cashIds, _]) => cashIds === cashCategory);
aimArray[1] = (Math.round(aimArray[1] * 100) - Math.round(cashValue * 100)) / 100;
});
}
cashInputElement.value = "";
priceElement.textContent = `Total: $${price}`;
cashInDrawer.innerHTML = `<p>Change in drawer:</p>${cid.map(([cashIds, cashUnit]) =>`<p>${capitalID[cashIds]}: $${cashUnit}</p>`).join("")}`;
};
purchaseBtn.addEventListener("click", confirmDueOver);
cashInputElement.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
confirmDueOver();
}
});
![Screenshot 2024-11-15 194732|410x500](upload://cCvi0tPfuGO13ZP5J0pt3qvgMln.jpeg)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register