Hi everyone, I’m having trouble in the last test of javascript algorithms and data structures, I would appreciate some help with my code, thanks in advance.
function checkCashRegister(price, cash, cid) {
let due = cash-price;
//console.log(din, due)
let arrV = [];
let sum = 0;
let tot = 0;
let ref = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01]
let ref_ = ref.filter(x => x <= due);
let cidr= cid.slice(0, ref_.length).reverse()
let din = cidr.reduce((acc, el) => parseFloat(acc)+parseFloat(el[1]),0);
//console.log(cidr, ref_)
//console.log(din, due)
if (din>due) {
for (let i = 0; tot < due; i++) {
//console.log(cidr[i][1], arrV, ref_[i])
if (cidr[i][1]+tot<due) {
arrV.push(cidr[i])
tot += cidr[i][1];
//console.log(arrV)
}
else {
while(cidr[i][1]+tot>due && cidr[i][1]>0.05) {
cidr[i][1] -= ref_[i];
}
arrV.push(cidr[i])
tot += cidr[i][1]
}
}
console.log(arrV, due, din)
console.log({status: 'OPEN', change: arrV})
return {status: 'OPEN', change: arrV}
}
else if (din === due) {
console.log({status: 'CLOSED', change: cid})
return {status: 'CLOSED', change: cid}
}
else {
console.log({status: 'INSUFFICIENT_FUNDS', change: []})
return {status: 'INSUFFICIENT_FUNDS', change: []}
}
}
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])