I have the cash register project complete. However, below 2 steps are failing, even though the correct output is displayed.
Any suggestions would be appreciated.
const cashInput = document.getElementById("cash")
const changeDueDiv = document.getElementById("change-due")
const purchaseBtn = document.getElementById("purchase-btn")
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]
];
let cidValue = [
['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.1],
['QUARTER', .25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE HUNDRED', 100]
];
// Step 12
// price = 3.26
// cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]];
// STEP 18
//price = 19.5
//cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
purchaseBtn.addEventListener("click", ()=>{
const cash = Number(Number(cashInput.value).toFixed(2));
const changeDue = Number(((price - cash) * -1).toFixed(2));;
const cidTotal = Number(cid.reduce((acc,el)=> acc + el[1],0).toFixed(2));
const changeDenominations = getChangeDenominations(changeDue);
const changeTotal = Number(changeDenominations.reduce((acc,el)=> acc + el[1],0).toFixed(2));
let status = getStatus(cash, changeDue, changeTotal, cidTotal, changeDenominations);
let changeTextOutput = status;
if(price > cash)
{
alert("Customer does not have enough money to purchase the item");
changeTextOutput = ""
}
if(Number(changeDue) === 0)
{
changeTextOutput = "No change due - customer paid with exact cash";
}
changeDueDiv.textContent = changeTextOutput;
})
const getChangeDenominations = (changeDue) => {
if(changeDue < 0)
{
return [];
}
const changeArray = []
const cidHighestToLowest = cidValue.reverse();
const cidReverse = cid.reverse();
cidHighestToLowest.forEach((item, index)=>{
const value = item[1];
const denomination = item[0];
if(changeDue > value)
{
const denominationAmount = Math.floor(changeDue/value) * value;
const cidValue = cidReverse[index][1];
if(cidValue >= denominationAmount)
{
changeArray.push([denomination,denominationAmount]);
changeDue = Number((changeDue - denominationAmount).toFixed(2));
}
else if(cidValue > 0)
{
// take all the change we can for this denomination
changeArray.push([denomination,cidValue]);
changeDue = Number((changeDue - cidValue).toFixed(2));
}
}
})
return changeArray
}
const createChangeDenominationString = (changeDenominations) =>
{
const changeDenominationsString =
changeDenominations.reduce((acc,el)=>{
return acc += `${el[0]}: \$${el[1]} `
}, "").trim();
return changeDenominationsString;
}
const getStatus = (cash, changeDue, changeTotal, cidTotal, changeDenominations) =>{
let status = "";
if(cidTotal < changeDue)
{
status = "Status: INSUFFICIENT_FUNDS";
}
if(price < cash && changeTotal < changeDue)
{
status = "Status: INSUFFICIENT_FUNDS";
}
else if(price < cash && cidTotal === changeDue)
{
status = `Status: CLOSED ${createChangeDenominationString(changeDenominations)}`;
}
else if(cidTotal > changeDue && changeDue !== 0)
{
status = `Status: OPEN ${createChangeDenominationString(changeDenominations)}`;
}
return status;
}
Step 12
12. When price
is 3.26
, the value in the #cash
element is 100
, cid
is [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]
, and the #purchase-btn
element is clicked, the value in the #change-due
element should be "Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04"
.
and Step 18. 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"
.