Please I need help, my code can’t pass test 12 and 13.
JavaScript Algorithms and Data Structure–Build a Cash Register Project
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 totalCidDisplay = document.getElementById(“total-cid”);
const change = document.getElementById(“change-due”);
const purchaseBtn = document.getElementById(“purchase-btn”);
const cidDisplay = document.getElementById(“cid-display”);
const cash = document.getElementById(“cash”)
const priceDisplay = document.getElementById(“price”)
cid.forEach(([currencyUnit, value])=>{
cidDisplay.innerHTML += `
${currencyUnit} : $${value}
`})
function changeGiver(arr, prize, cash){
change.style.display = “block”
priceDisplay.textContent = `Price: $${price}`
const currencyUnits = [
[“PENNY”, 0.01],
[“NICKEL”, 0.05],
[“DIME”, 0.10],
[“QUARTER”, 0.25],
[“ONE”, 1.00],
[“FIVE”, 5.00],
[“TEN”, 10.00],
[“TWENTY”, 20.00],
[“ONE HUNDRED”, 100]
];
let changeDue = (cash - prize).toFixed(2);
let totalCid = parseFloat(arr.reduce((sum, [_, amount]) => sum + amount, 0)).toFixed(2);
if (cash < prize) {
alert(“Customer does not have enough money to purchase the item”);
}
else if (cash === prize) {
alert(“No change due - customer paid with exact cash”)
change.textContent = “No change due - customer paid with exact cash”
}
let changeArray =
let remainingChange = parseFloat(changeDue)
for(let i = currencyUnits.length-1; i>= 0; i–){
const [denom, unitValue] = currencyUnits[i];
let available = arr[i][1]
let used = 0
while(remainingChange >= unitValue && available > 0){
remainingChange = (remainingChange - unitValue).toFixed(2)
available = (available - unitValue).toFixed(2)
used += unitValue
}
if (used > 0){
changeArray.push([denom, used.toFixed(2)])
arr[i][1] = available
}
}
remainingChange = parseFloat(remainingChange)
if (remainingChange > 0){
let canRemains = false
for (let i = 0; i<currencyUnits.length && !canRemains; i++){
const [_, unitValue] = currencyUnits[i]
const available = arr[i][1]
if(available >=remainingChange){
canRemains = true}
}
if (totalCid < changeDue || !canRemains){
change.textContent = “Status: INSUFFICIENT_FUNDS”
}
changeArray =
remainingChange = parseFloat(changeDue)
for (let i = currencyUnits.length-1; i >= 0; i–){
const [denom, unitValue] = currencyUnits[i]
let available = arr[i][1]
let used = 0
while(remainingChange >= unitValue && available >0){
remainingChange = (remaining - unitValue).toFixed(2)
available = (available - unitValue).toFixed(2)
used += unitValue
}
if (used > 0){
changeArray.push([denom, used.toFixed(2)])
}
}
remainingChange = parseFloat(remainingChange)
if (remainingChange > 0){
change.textContent = “Status: INSUFFICIENT_FUNDS”
}
}
let totalChange = changeArray.reduce((sum, [_, amount]) => sum + parseFloat(amount), 0).toFixed(2);
if (totalCid === changeDue) {
change.textContent = “Status: CLOSED " + changeArray.map(([denom, amount]) => `${denom}: $${amount}`).join(” ");
} else if (totalChange < changeDue) {
change.textContent = “Status: INSUFFICIENT_FUNDS”;
}
else if (cash > prize && totalCid > changeDue){
change.textContent = “Status: OPENED " + changeArray.map(([denom, amount]) => `${denom}: $${amount}`).join(” ");
}
};
purchaseBtn.addEventListener(“click”, ()=>{
changeGiver(cid,price, parseFloat(cash.value))
})