Hey, i am working on this project but i cant figure out why the “Status CLOSED” wont be displayed.
I found the sum of the cid values and i used an if statement to check if the cash.value === sum.
Here is what i have written so far:
let cash = document.getElementById("cash");
let btn = document.getElementById("purchase-btn");
let change = document.getElementById("change-due");
let drawer = document.getElementById("cash-in-drawer");
let price = 1.87;
// let price = 0.5;
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]
];
function displayDrawer() {
for(let i = 0; i < cid.length; i++) {
let el = document.createElement('p');
el.textContent = `${cid[i][0]}: ${cid[i][1]}`;
drawer.appendChild(el)
}
}
let sum = 0;
for(let k = 0; k < cid.length; k++){
sum += cid[k][1];
}
sum = Math.round(sum * 100) / 100;
console.log(sum);
btn.addEventListener("click", () => {
change.textContent = "";
if(cash.value < price){
alert("Customer does not have enough money to purchase the item");
cash.value = "";
}else if(cash.value == price) {
change.textContent = "No change due - customer paid with exact cash";
cash.value = "";
}else if(cash.value === sum){
change.textContent = "Status: CLOSED";
cash.value = "";
}else if(cash.value > price && cash.value !== sum) {
let changeStatus = document.createElement('p');
changeStatus.textContent = "Status: OPEN";
change.appendChild(changeStatus);
const reversedCid = cid.map(item => item[1]).reverse();
// [100, 60, 20, 55, 90, 4.25, 3.1, 2.05, 1.01]
const billValue = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
let cashToChange = cash.value - price;
let i = 0;
while(i < billValue.length){
if(cashToChange >= billValue[i] && reversedCid[i] > 0){
cashToChange -= billValue[i];
reversedCid[i] -= billValue[i];
}else{
i++;
}
}
if(cashToChange > 0){
change.textContent = "Status: INSUFFICIENT_FUNDS"
}
}
})
displayDrawer();