Tell us what’s happening: Okay, so I have been at this for two days. I keep getting it to where it will almost be perfect, but everything will work except the alert and messages and I can’t figure out why. Please help in any way you can.
const cashInput = document.getElementById('cash');
const changeDue = document.getElementById('change-due');
const purchaseBtn = document.getElementById('purchase-btn');
const cidDisplay = document.getElementById('cash-in-drawer');
const purchaseCalculation = () => {
let price;
let cid;
price = 19.5;
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 cash = parseFloat(cashInput.value);
let change = cash - price;
if (cash < price) {
alert('Customer does not have enough money to purchase the item');
return;
}
if (cash === price) {
changeDue.textContent = "No change due - customer paid with exact cash";
return;
}
let changeArr = [];
let cidTotal = 0;
for (let [_, amount] of cid) {
cidTotal += amount;
}
if (change > cidTotal) {
changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
return;
} else if (change === cidTotal) {
changeDue.textContent = "Status: CLOSED";
return;
}
for (let i = cid.length - 1; i >= 0; i--) {
const [unit, amount] = cid[i];
const unitValue = parseFloat(unit === "ONE HUNDRED" ? 100 : unit === "TWENTY" ? 20 : unit === "TEN" ? 10 : unit === "FIVE" ? 5 : unit === "ONE" ? 1 : unit === "QUARTER" ? 0.25 : unit === "DIME" ? 0.1 : unit === "NICKEL" ? 0.05 : 0.01);
let numUnits = Math.min(Math.floor(change / unitValue), amount);
if (numUnits > 0) {
changeArr.push([unit, numUnits * unitValue]);
change = Math.round((change - numUnits * unitValue) * 100) / 100;
}
}
let changeMessage = "Status: OPEN ";
for (let i = 0; i < changeArr.length; i++) {
changeMessage += `${changeArr[i][0]}: $${changeArr[i][1]} `;
}
changeDue.textContent = changeMessage;
};
purchaseBtn.addEventListener("click", purchaseCalculation);
cidDisplay.innerHTML = "<p><strong>Cash in Drawer:</strong></p>";
cid.forEach(item => {
cidDisplay.innerHTML += `<p>${item[0]}: $${item[1]}</p>`;
});
WARNING
The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.
You will need to take an additional step here so the code you wrote presents in an easy to read format.
Please copy/paste all the editor code showing in the challenge from where you just linked.
Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register