This is my code, i think it does everything it needs to do, but I don’t pass… Can someone have a look at it? Thanks! They ask: if price = 19.5 and cash = 20, the value in changeDue needs to be: Status: OPEN QUARTER: $0.5
It is exactly that
let price = 19.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]
];
const valutaValue = [
['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.1],
['QUARTER', 0.25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE HUNDRED', 100]
];
const cash = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purBtn = document.getElementById("purchase-btn");
const checkBeforePurchase = () => {
}
//calculate change
cid.reverse()
valutaValue.reverse();
const calculateChange = () => {
let changeAmount = cash.value - price;
let changeArr = [];
let insufficientFunds = false;
for (let i = 0; i < cid.length; i++){
let coinName = valutaValue[i][0];
let coinValue = valutaValue[i][1];
let coinAvailable = cid[i][1];
let amountNeeded = 0;
while (changeAmount >= coinValue && coinAvailable > coinValue){
changeAmount -= coinValue;
changeAmount = Number(changeAmount.toFixed(2));
coinAvailable -= coinValue;
coinAvailable = Number(coinAvailable.toFixed(2));
amountNeeded += coinValue;
amountNeeded = Number(amountNeeded.toFixed(2));
}
cid[i][1] = coinAvailable;
if (amountNeeded > 0) {
changeArr.push(`${coinName}: \$${amountNeeded}`);
}
if (changeAmount > 0 && coinAvailable < coinValue) {
insufficientFunds = true;
}
}
if (changeAmount === 0 && !insufficientFunds) {
changeDue.textContent = `Status: OPEN ${changeArr.join(" ")}`;
} else {
changeDue.textContent = `Status: OPEN INSUFFICIENT_FUNDS`;
}
if (cash.value < price){
alert("Customer does not have enough money to purchase the item")
} if (cash.value == price){
changeDue.textContent = "No change due - customer paid with exact cash";
}
cash.value = "";
}
//Check all functions
const allFunctions = (e) => {
e.preventDefault
calculateChange();
}
purBtn.addEventListener("click", allFunctions);