I’m not sure why the test case - When the value in the #cash
element is less than price
, an alert should appear with the text "Customer does not have enough money to purchase the item"
is not passing when this test case passes - When price
is 20
and the value in the #cash
element is 10
, an alert should appear with the text "Customer does not have enough money to purchase the item"
let price = 20;
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 cash = document.getElementById('cash');
const purchaseBtn = document.getElementById('purchase-btn');
const changeDue = document.getElementById('change-due');
function totalCid() {
let res = 0;
for (let i = 0; i < cid.length; i++) {
res += Number(cid[i][1]);
}
return res.toFixed(2);
}
purchaseBtn.addEventListener('click', () => {
let cidv = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let change = (parseFloat(cash.value) - price).toFixed(2);
let res = {status: 'Status: OPEN', change: ''};
const updateRes = () => `${res.status} ${res.change}`;
cidv.reverse();
if (parseFloat(cash.value) < price) {
alert("Customer does not have enough money to purchase the item");
return;
} else if (parseFloat(cash.value) === price) {
changeDue.textContent = 'No change due - customer paid with exact cash';
return;
}
console.log(`${change} > ${totalCid()} = ${change > totalCid()}`);
if (change > totalCid()) {
res.status = 'Status: INSUFFICIENT_FUNDS';
changeDue.textContent = res.status;
return;
} else if (change === totalCid()) {
res.status = 'Status: CLOSED';
}
for (let i = 0; i < cid.length; i++) {
while (cidv[i] <= change && cid[i][1] - cidv[i] >= 0) {
change = (change - cidv[i]).toFixed(2);
cid[i][1] = (cid[i][1] - cidv[i]).toFixed(2);
}
}
console.log(cid);
if (change > 0) {
res.status = 'Status: INSUFFICIENT_FUNDS';
changeDue.textContent = res.status;
return;
}
});