Tell us what’s happening:
The Last Checklist of Project Doesn’t Get Checked Even My Output Is Right
const purchaseBtn = document.getElementById("purchase-btn");
const changeContainer = document.getElementById("change-due");
const cidContainer = document.getElementById("cash-in-drawer");
let price = 19.5;
let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
const render = () => {
cid.forEach(element => {
cidContainer.innerHTML += `<p>${element[0]}: $${element[1]}</p>`;
})
}
render();
const countCid = (cid) => {
let totalCidTemp = 0;
cid.forEach((element) => {
totalCidTemp = (totalCidTemp + parseFloat(element[1]));
})
return totalCidTemp;
}
const countChange = (cash, price, cid) => {
let changeDue = (cash - price).toFixed(2);
let change = {}
if (Math.floor((changeDue / 100)) > 0) {
change["ONE HUNDRED"] = 0;
}
if (Math.floor((changeDue / 20)) > 0) {
change["TWENTY"] = 0;
}
if (Math.floor((changeDue / 10)) > 0) {
change["TEN"] = 0;
}
if (Math.floor((changeDue / 5)) > 0) {
change["FIVE"] = 0;
}
if (Math.floor((changeDue / 1)) > 0) {
change["ONE"] = 0;
}
if (Math.floor((changeDue / 0.25)) > 0) {
change["QUARTER"] = 0;
}
if (Math.floor((changeDue / 0.1)) > 0) {
change["DIME"] = 0;
}
if (Math.floor((changeDue / 0.05)) > 0) {
change["NICKEL"] = 0;
}
if (Math.floor((changeDue / 0.01)) > 0) {
change["PENNY"] = 0;
}
while (changeDue > 0) {
if (Math.floor((changeDue / 100)) > 0 && cid[8][1] > 0) {
change["ONE HUNDRED"] = change["ONE HUNDRED"] + 100;
cid[8][1] = (cid[8][1] - 100).toFixed(2);
changeDue = (changeDue - 100).toFixed(2);
} else if (Math.floor((changeDue / 20)) > 0 && cid[7][1] > 0) {
change["TWENTY"] = change["TWENTY"] + 20;
cid[7][1] = (cid[7][1] - 20).toFixed(2);
changeDue = (changeDue - 20).toFixed(2);
} else if (Math.floor((changeDue / 10)) > 0 && cid[6][1] > 0) {
change["TEN"] = change["TEN"] + 10;
cid[6][1] = (cid[6][1] - 10).toFixed(2);
changeDue = (changeDue - 10).toFixed(2);
} else if (Math.floor((changeDue / 5)) > 0 && cid[5][1] > 0) {
change["FIVE"] = change["FIVE"] + 5;
cid[5][1] = (cid[5][1] - 5).toFixed(2);
changeDue = (changeDue - 5).toFixed(2);
} else if (Math.floor((changeDue / 1)) > 0 && cid[4][1] > 0) {
change["ONE"] = change["ONE"] + 1;
cid[4][1] = (cid[4][1] - 1).toFixed(2);
changeDue = (changeDue - 1).toFixed(2);
} else if (Math.floor((changeDue / 0.25)) > 0 && cid[3][1] > 0) {
change["QUARTER"] = change["QUARTER"] + 0.25;
cid[3][1] = (cid[3][1] - 0.25).toFixed(2);
changeDue = (changeDue - 0.25).toFixed(2);
} else if (Math.floor((changeDue / 0.1)) > 0 && cid[2][1] > 0) {
change["DIME"] = change["DIME"] + 0.1;
cid[2][1] = (cid[2][1] - 0.1).toFixed(2);
changeDue =(changeDue - 0.1).toFixed(2);
} else if (Math.floor((changeDue / 0.05)) > 0 && cid[1][1] > 0) {
change["NICKEL"] = change["NICKEL"] + 0.05;
cid[1][1] = (cid[1][1] - 0.05).toFixed(2);
changeDue = (changeDue - 0.05).toFixed(2);
} else if (Math.floor((changeDue / 0.01)) > 0 && cid[0][1] > 0) {
change["PENNY"] = parseFloat((change["PENNY"] + 0.01).toFixed(2));
cid[0][1] = (cid[0][1] - 0.01).toFixed(2);
changeDue = (changeDue - 0.01).toFixed(2);
} else {
return false
}
}
return change;
}
const getStatus = (totalCid, cash, changeResult) => {
if (totalCid > cash - price && changeResult !== false) {
return "OPEN"
} else if (totalCid > cash - price && changeResult === false) {
return "INSUFFICIENT_FUNDS"
}
else if (totalCid < cash - price && changeResult === false) {
return "INSUFFICIENT_FUNDS"
} else if (totalCid === cash - price) {
return "CLOSED"
}
}
purchaseBtn.addEventListener("click", (e) => {
const cash = parseFloat(document.getElementById("cash").value);
let totalCid = countCid(cid);
const change = countChange(cash, price, cid)
const status = getStatus(totalCid, cash, change);
changeContainer.innerHTML = "";
cidContainer.innerHTML = "";
if (cash < price) {
alert("Customer does not have enough money to purchase the item");
return;
}
if (cash === price) {
changeContainer.innerHTML = `<p>No change due - customer paid with exact cash</p>`;
} else if (status === "OPEN" || status === "CLOSED") {
changeContainer.innerHTML += `<p>Status: ${status}</p>`;
for (const key in change) {
changeContainer.innerHTML += `<p>${key}: $${change[key]}</p>`;
}
} else if (status === "INSUFFICIENT_FUNDS") {
changeContainer.innerHTML = `<p>Status: ${status}</p>`;
}
cid.forEach(element => {
cidContainer.innerHTML += `<p>${element[0]}: $${element[1]}</p>`;
})
})
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register