This test case is failing when running tests, although it passes manually.
When price
is 19.5
, the value in the #cash
element is 20
, cid
is [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
, and the #purchase-btn
element is clicked, the value in the #change-due
element should be "Status: CLOSED PENNY: $0.5"
.
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 values = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
];
let sum = 0;
const registerContainer = document.getElementById("register-container");
const changeDue = document.getElementById("change-due");
const amountDue = document.getElementById("amount-due");
const purchaseBtn = document.getElementById("purchase-btn");
const getChange = () => {
const cash = document.getElementById("cash");
changeDue.textContent = "";
if (cash.value === "") {
alert("Please enter a cash amount.");
return;
}
if (price == cash.value) {
changeDue.textContent = "No change due - customer paid with exact cash";
return;
}
if (cash.value < price) {
alert("Customer does not have enough money to purchase the item");
return;
}
sum = 0;
for (let i = 0; i < cid.length; i++) {
sum += cid[i][1];
}
sum = Math.round(sum * 100) / 100;
console.log(`sum: ${sum}`);
if (sum < cash.value - price) {
changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
return;
}
let changeDueAmount = cash.value - price;
let currValuesIdx = values.length - 1;
let change = [];
while (changeDueAmount > 0 && currValuesIdx >= 0) {
let temp = changeDueAmount;
let count = Math.floor(changeDueAmount / values[currValuesIdx][1]);
let remainder = changeDueAmount % values[currValuesIdx][1];
if(remainder < 0.01) {
remainder = 0;
}
if (count === 0) {
currValuesIdx--;
continue;
}
if (cid[currValuesIdx][1] === 0) {
changeDueAmount = temp;
currValuesIdx--;
continue;
}
if (count * values[currValuesIdx][1] > cid[currValuesIdx][1]) {
if (cid[currValuesIdx][1] !== 0) {
remainder += count * values[currValuesIdx][1] - cid[currValuesIdx][1];
}
change.push([values[currValuesIdx][0], Math.floor(cid[currValuesIdx][1] / values[currValuesIdx][1])]);
cid[currValuesIdx][1] = 0;
}
else {
cid[currValuesIdx][1] -= count * values[currValuesIdx][1];
change.push([values[currValuesIdx][0], count]);
}
currValuesIdx--;
changeDueAmount = remainder;
changeDueAmount = Math.round(changeDueAmount * 100) / 100;
}
if(changeDueAmount > 0) {
changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
return;
}
if (sum === 0) {
changeDue.textContent = "Status: CLOSED";
}
else { // register still has money
changeDue.textContent = "Status: OPEN"
}
change.forEach((el) => {
let value = getDollarAmount(el[0], el[1])
changeDue.textContent += ` ${el[0]}: \$${value}`
});
};
const getDollarAmount = (denomination, count) => {
for (let i = 0; i < values.length; i++) {
if (values[i][0] === denomination) {
return count * values[i][1];
}
}
return 0;
}
purchaseBtn.addEventListener("click", getChange);
Challenge Information:
Build a Cash Register Project - Build a Cash Register