My code works for everything until I input a “cash” value that is greater than 37. I don’t understand why that specific value and I have looked for mistakes in the code, but as I said it works for all but one test check. The code runs correctly but not the if statement in the purchase function at a value greater than 37. Any guidance is appreciated!
let price = 1.87;
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 currencyUnits = [
['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.1],
['QUARTER', 0.25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE HUNDRED', 100]
];
const changeDueElement = document.getElementById("change-due");
const cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const totalPriceElement = document.getElementById("price-total");
const cashInDrawerDiv = document.getElementById("cash-in-drawer");
const cashInDrawerSpans = document.querySelectorAll("#cash-in-drawer span");
const changeDueArr = [
['PENNY', 0],
['NICKEL', 0],
['DIME', 0],
['QUARTER', 0],
['ONE', 0],
['FIVE', 0],
['TEN', 0],
['TWENTY', 0],
['ONE HUNDRED', 0]
];
totalPriceElement.textContent = `Total: $${price}`;
cashInDrawerSpans.forEach((span, index) => span.innerText += cid[index][1]);
const arrSum = (arr) => {
let sum = 0;
for (let i = 0; i < arr.length; i++){
let num = arr[i][1];
sum += num;
}
return sum.toFixed(2);
}
const calculateChange = () => {
let number = parseFloat(cash.value);
if (number < price) {
alert("Customer does not have enough money to purchase the item");
} else if (number === price) {
changeDueElement.textContent = "No change due - customer paid with exact cash";
} else {
const changeDue = number - price;
return changeDue.toFixed(2);
}
}
const updateCID = (cid, changeDueArr) => {
for (let i = 0; i < cid.length; i++) {
cid[i][1] -= changeDueArr[i][1];
cashInDrawerSpans[i].innerText = cid[i][1].toFixed(2);
}
}
const findDenominations = (num) => {
for (let i = currencyUnits.length - 1; i >= 0; i--) {
let currentDenominationValue = currencyUnits[i][1];
let quotient = Math.floor(num / currentDenominationValue);
let denominationFitsIn = currentDenominationValue * quotient;
let currentDenominationValueInRegister = cid[i][1];
if (denominationFitsIn <= currentDenominationValueInRegister) {
num = (num - denominationFitsIn).toFixed(2);
changeDueArr[i][1] = denominationFitsIn;
} else {
num = (num - currentDenominationValueInRegister).toFixed(2);
changeDueArr[i][1] = currentDenominationValueInRegister;
}
console.log(`${num}`);
}
console.log(changeDueArr);
console.log(num);
return Number(num);
}
const displayChangeDueArr = (changeDueArr) => {
for (let i = changeDueArr.length - 1; i >= 0; i--) {
let label = changeDueArr[i][0];
let value = changeDueArr[i][1];
if (value !== 0) {
changeDueElement.textContent += ` ${label}: $${value}`;
}
}
}
const purchase = () => {
let totalChangeDue = calculateChange();
console.log(totalChangeDue);
let sumCID = arrSum(cid);
console.log(sumCID);
let leftover = findDenominations(totalChangeDue);
if (totalChangeDue === sumCID && leftover === 0) {
changeDueElement.textContent = "Status: CLOSED";
displayChangeDueArr(changeDueArr);
updateCID(cid, changeDueArr);
console.log("This block ran 1");
} else if (totalChangeDue < sumCID && leftover === 0) {
changeDueElement.textContent = "Status: OPEN";
displayChangeDueArr(changeDueArr);
updateCID(cid, changeDueArr);
console.log("This block ran 2");
} else if (totalChangeDue > sumCID && leftover > 0) {
changeDueElement.textContent = "Status: INSUFFICIENT_FUNDS";
console.log("This block ran 3");
}
}
purchaseBtn.addEventListener("click", purchase);