Good morning, please, could you help me, I’ve been trying to understand my mistake for days and I didn’t find the solution on the forum, mainly because it’s a private project, I searched on the internet and when I correct one error, I find another, and in the end, I stay with these 2 errors
I appreciate the super help
ERROR:
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”.
When price is less than the value in the #cash element, total cash in drawer cid is equal to change due, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: CLOSED” with change due in coins and bills sorted in highest to lowest order.
const cashInput = document.getElementById("cash");
const changeText = document.getElementById("change-due");
const button = document.getElementById("purchase-btn");
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]
];
const conversion = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100,
};
const totalCash = Number(cid.reduce((acc, cur) => acc + cur[1], 0).toFixed(2));
const moneyChecker = (changeDue, statusCheck) => {
let results = {
status: statusCheck,
change: []
};
for (let i = cid.length - 1; i >= 0; i--) {
if (changeDue >= conversion[cid[i][0]] && changeDue > 0) {
const timesInto = Math.floor(changeDue / conversion[cid[i][0]]);
const amount = timesInto * conversion[cid[i][0]];
if (amount > cid[i][1]) {
changeDue -= cid[i][1];
results.change.push([cid[i][0], cid[i][1]]);
} else {
changeDue -= amount;
results.change.push([cid[i][0], amount]);
}
changeDue = changeDue.toFixed(2);
}
}
if (changeDue > 0) {
changeText.textContent = "Status: INSUFFICIENT_FUNDS";
return;
}
results.change.sort((a, b) => conversion[b[0]] - conversion[a[0]]);
let changeOutput = results.change.map(item => `${item[0]}: $${item[1].toFixed(2)}`);
changeText.textContent = `Status: ${results.status} ${changeOutput.join(" ")}`;
};
button.addEventListener("click", () => {
const cash = Number(cashInput.value);
let changeDue = (cash - price).toFixed(2);
if (totalCash < changeDue) {
changeText.textContent = "Status: INSUFFICIENT_FUNDS";
} else if (cash < price) {
alert("Customer does not have enough money to purchase the item");
} else if (cash === price) {
changeText.textContent = "No change due - customer paid with exact cash";
} else if (totalCash === changeDue) {
moneyChecker(changeDue, "CLOSED");
} else {
moneyChecker(changeDue, "OPEN");
}
});