Need Help finishing this challenge. I can get all the test to solve for the cash register challenge in the JavaScript Legacy Certification curriculum, except for test 3. I tried looking for help, ask some friends, re-set the tests and even tried using AI to debug the code. Nothing seems to be working. Any ideas will be helpful and appreciated!
function checkCashRegister(price, cash, cid) {
// Define the currency units and their values in descending order
const currencyUnitValues = [
["ONE HUNDRED", 100],
["TWENTY", 20],
["TEN", 10],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["DIME", 0.1],
["NICKEL", 0.05],
["PENNY", 0.01]
];
// Calculate the total change to return
let changeDue = cash - price;
let change = [];
let totalCid = 0;
// Calculate the total cash in the drawer
for (let i = 0; i < cid.length; i++) {
totalCid += cid[i][1];
}
// Round to two decimal places for accuracy when working with currency
changeDue = Math.round(changeDue * 100) / 100;
totalCid = Math.round(totalCid * 100) / 100;
// Check if we have enough cash in the drawer
if (totalCid < changeDue) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
// Check if the drawer has the exact change
if (totalCid === changeDue) {
return { status: "CLOSED", change: cid };
}
// Process the change due from the cash-in-drawer
for (let i = 0; i < currencyUnitValues.length; i++) {
const [unit, value] = currencyUnitValues[i];
let amountInDrawer = cid[i][1];
let amountToReturn = 0;
// Calculate the amount to return for the current currency unit
while (changeDue >= value && amountInDrawer > 0) {
changeDue = Math.round((changeDue - value) * 100) / 100;
amountInDrawer = Math.round(amountInDrawer * 100) / 100;
amountToReturn += value;
}
// If we return some change, update the drawer and change array
if (amountToReturn > 0) {
change.push([unit, amountToReturn]);
cid[i][1] -= amountToReturn;
}
// If the change due has been resolved, stop the loop
if (changeDue === 0) break;
}
// Check if we were able to return the exact change
if (changeDue > 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
// If there is still money left in the drawer, return OPEN status
return { status: "OPEN", change: change };
}