Tell us what’s happening:
My cash register project code is admittedly messy, but it should technically pass all the tests. And it is passing all of them but one: question 17:
- When price is less than the value in the #cash element, total cash in drawer cid is greater than change due, but the individual denomination amounts make it impossible to return needed change, when the #purchase-btn element is clicked, the value in the #change-due element should be "Status: INSUFFICIENT_FUNDS”
When I run the code with this exact scenario, the status message does indeed appear as it should. So I don’t understand why the test won’t pass? Does anyone have any ideas?
Including my code below:
Your code so far
<!-- file: index.html -->
/* file: script.js */
//// UI variables
const changeDueDisplay = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const totalDisplay = document.getElementById("total");
const currencyAmounts = document.getElementsByClassName("currency-amounts");
//// other variables
// price of product customer wants to buy
let price = 19.5;
// cash in drawer
let cid = [
['PENNY', 0.5],
['NICKEL', 0],
['DIME', 0],
['QUARTER', 0],
['ONE', 0],
['FIVE', 1],
['TEN', 1],
['TWENTY', 1],
['ONE HUNDRED', 01]
];
const denominationsIC = [1, 5, 10, 25, 100, 500, 1000, 2000, 10000];
// display cost of item on page (in "total" element)
totalDisplay.textContent = `Total: $${price}`;
// display current cash in drawer
for (let i = 0; i < 9; i++) {
currencyAmounts[i].textContent = `$${Math.round(cid[i][1] * 100) / 100}`;
}
//////////
const main = (cash) => {
// multiply by 100 to avoid rounding errors; IC stands for "in cents"
const cashIC = cash * 100; // amount received from customer
const priceIC = price * 100; // cost of item they want to buy
const diffIC = cashIC - priceIC; // amount of change we owe them
const totalCIDIC = (cid[0][1] + cid[1][1] + cid[2][1] + cid[3][1] + cid[4][1] + cid[5][1] + cid[6][1] + cid[7][1] + cid[8][1]) * 100; // all the money we have available
// the total change we will give them, by denomination
let changeDue = [];
// variable to track cash register status
let status = "";
if (cashIC < priceIC) {
// If the customer does not have enough money for the purchase, log that
alert("Customer does not have enough money to purchase the item");
} else if (cashIC === priceIC) {
// If the customer paid with exact change, log that
status = "No change due - customer paid with exact cash";
} else {
// Otherwise, the customer is owed a certain amount of change
if (diffIC > totalCIDIC) {
// If we don't have enough money in the register, set status to insufficient
status = "Status: INSUFFICIENT_FUNDS";
changeDue = false;
} else if (diffIC === totalCIDIC) {
// If we have exactly enough and no more, set status to closed
status = "Status: CLOSED";
changeDue = calculate(diffIC);
} else {
// If we have more than enough, calculate change due
changeDue = calculate(diffIC);
if (!changeDue) {
status = "Status: INSUFFICIENT_FUNDS";
} else if (totalCIDIC === 0) {
status = "Status: CLOSED";
} else {
status = "Status: OPEN";
}
}
}
updateStatus(status);
updateDisplay(changeDue);
}
const calculate = (diffIC) => {
// variables to track calculation
const totalChangeDueIC = diffIC;
let remainderOwedIC = diffIC;
let cashToHandBackIC = 0;
let denominationsToHandBack = [];
let howMany = 0;
// time to calculate!
while (cashToHandBackIC < totalChangeDueIC) {
// loop through cid, largest denomination to smallest
for (let i = 8; i >= 0; i--) {
// if that DENOMINATION is < remainderOwedIC AND cash in this drawer is > 0:
if ((denominationsIC[i] <= remainderOwedIC) && (cid[i][1] > 0)) {
// first check if we've reached the pennies & there aren't enough
if (i === 0 && cid[0][1] * 100 < remainderOwedIC && cashToHandBackIC === 0) {
// if so, change variables as necessary and return early
denominationsToHandBack = [];
return false;
}
// ...then WHILE both those things remain true:
while ((denominationsIC[i] <= remainderOwedIC) && (cid[i][1] > 0)) {
// add 1x that denomination to the cash to hand back
cashToHandBackIC += denominationsIC[i];
// and subtract 1x that denomination from the remainder still owed
remainderOwedIC -= denominationsIC[i];
// also subtract it from that drawer of the cash register!
cid[i][1] -= denominationsIC[i] / 100;
// track how many of this denomination is needed
howMany++;
}
}
// add how many of that denomination were needed to the hand-back array
denominationsToHandBack[i] = howMany;
// and reset the "how many" tracker before moving to the next denomination
howMany = 0;
}
}
if (denominationsToHandBack.length > 0) {
return denominationsToHandBack;
} else {
return false;
}
}
const updateStatus = (status) => {
// show status message as first p child of changeDue div
changeDueDisplay.innerHTML += `<p>${status}</p>`;
}
const updateDisplay = (changeArray) => {
if (changeArray) {
// loop through changeArray; for each item...
for (let i = 8; i >= 0; i--) {
// ...if the value is more than zero...
if (changeArray[i] > 0) {
// ...add p child showing it
changeDueDisplay.innerHTML += `<p>${cid[i][0]}: $${changeArray[i] * denominationsIC[i] / 100}</p>`;
}
}
// also update each cash register drawer to show the new amount
for (let i = 0; i < 9; i++) {
currencyAmounts[i].textContent = `$${Math.round(cid[i][1] * 100) / 100}`;
}
}
}
purchaseBtn.addEventListener("click", () => {
// reset changeDue element to empty at start
changeDueDisplay.innerHTML = "";
// get value of cash received as payment from customer
let cash = document.getElementById("cash").value;
// run main function
main(cash);
// reset input field to empty
document.getElementById("cash").value = "";
});
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:135.0) Gecko/20100101 Firefox/135.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register