I wrote the code and tried it multiple times nothing seems to work if anyone have any suggestion
here is my code:;
//code
const buyButton=document.getElementById("purchase-btn")
const change=document.getElementById("change-due")
//values
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]];
//for cid worth
const equiv=[0.01,0.05,0.1,0.25,1,5,10,20,100]
for(let c=0;c<cid.length;c++){
cid[c][2]=Math.ceil(cid[c][1]/equiv[c])
cid[c][1]=equiv[c]
}
const cidR=cid.reverse()
console.log(cidR)
//check
let sum=0
const check=()=>{
cidR.forEach(num=>
{if(num[2]!==0){sum++}
console.log(sum)}
)}
// to get the index and times
const formula=()=>{
const cash=document.getElementById("cash").value
//change
if(cash-price>0){
let status
let text
let remain=cash-price
for(let c=0; c<7&&remain!==0; c++){
const finder=cidR.findIndex(value=>
value[1]<=remain&&value[2]>0)
if(finder>=0){
let times=Math.floor(remain/cidR[finder][1])
if(times>cid[finder][2])(times--)
cidR[finder][2]-=Math.floor(remain/cidR[finder][1])
remain-=cidR[finder][1]*times
remain=remain.toFixed(2)
text+=cid[finder][0]+': '+'$'+cidR[finder][1] *times+' '
console.log(cidR[finder])}
else if(finder<0&&remain>0){change.textContent="Status: INSUFFICIENT_FUNDS"}}
check()
if(sum==0&&remain==0){
status="CLOSED"
change.textContent=status+' '+text.replace('undefined','')
}else if(sum!==0&&remain==0){
status="OPEN"
change.textContent=
`Status: ${status} ${text.replace('undefined','')}`
}
}
//equal
else if(cash==price){change.textContent="No change due - customer paid with exact cash"}
//no money
else if(cash<price){alert("Customer does not have enough money to purchase the item")}
}
//button
buyButton.addEventListener('click',formula)