I’m working on the different “status” messages that show insufficient funds/open/closed for the register. The open/insufficient funds part is working, but I run into a bug with the closed part. I am able to do toChange = parseFloat(totalChange).ToFixed(2)
to achieve the ‘closed’ requirement, but the status starts to display “Insufficient funds” if I input any number that is less than or equal to 11. When the number is greater than 11, it’s able to work. Happens with some other numbers as well, such as 101, 55, 44, 99. When I remove the toFixed(2), the code goes back to working how it should, but then I’m not able to meet the ‘status: closed’ requirement because it doesn’t find an equal number. Anyone able to point me in the direction of what I need to fix?
const cashInput = document.getElementById("cash");
let changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const changeInDrawer = document.getElementById("change-in-drawer")
let priceDisplay = document.getElementById("price")
let fundStatus = document.getElementById("status");
let price = 1.87;
priceDisplay.textContent = `Price: $${price}`
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]
];
changeInDrawer.innerHTML = `
<p>Change In Drawer: </p>
<p>pennies: $${cid[0][1]}</p>
<p>nickels: $${cid[1][1]}</p>
<p>dimes: $${cid[2][1]}</p>
<p>quarters: $${cid[3][1]} </p>
<p>ones: $${cid[4][1]}</p>
<p>fives: $${cid[5][1]}</p>
<p>tens: $${cid[6][1]}</p>
<p>twenties: $${cid[7][1]}</p>
<p>hundreds: $${cid[8][1]} </p>
`
const verifyChange = (e) => {
if (parseFloat(cashInput.value) < price) {
alert("Customer does not have enough money to purchase the item");
}
else if (parseFloat(cashInput.value) > price) {
let calculateChange = (parseFloat(cashInput.value) - price).toFixed(2);
changeDue.textContent = "Total: $" + calculateChange;
let totalChange = cid.map(function(v, index) { return v[1] }).reduce(function (a, b) { return a + b })
totalChange = parseFloat(totalChange);
if (totalChange < calculateChange) {
fundStatus.textContent = 'Status: INSUFFICIENT_FUNDS';
}
else if (totalChange > calculateChange) {
fundStatus.textContent = 'Status: OPEN'
}
else if (totalChange === calculateChange) {
fundStatus.textContent = 'Status: CLOSED'
}
}
else if (parseFloat(cashInput.value) == price) {
changeDue.textContent = "No change due - customer paid with exact cash"
}
}
purchaseBtn.addEventListener('click', verifyChange);type or paste code here