Tell us what’s happening:
i have tried everything, unable to pass these test cases. Please help me
Your code so far
function checkCashRegister(price, cash, cid) {
const currencyUnits = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1.0],
["FIVE", 5.0],
["TEN", 10.0],
["TWENTY", 20.0],
["ONE HUNDRED", 100.0]
];
let changeDue = cash - price;
let totalCid = cid.reduce((sum, [_, amount]) => sum + amount, 0);
// If total cash in drawer is less than the change due, or exact change cannot be given
if (totalCid < changeDue) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
// If total cash in drawer is equal to the change due
if (totalCid === changeDue) {
return { status: "CLOSED", change: cid };
}
cid = cid.reverse(); // Reverse to start giving change from highest to lowest denomination
let changeArray = [];
for (let i = 0; i < currencyUnits.length; i++) {
const [currency, currencyValue] = currencyUnits[i];
let amountInDrawer = cid[i][1];
let amountToReturn = 0;
while (changeDue >= currencyValue && amountInDrawer > 0) {
changeDue -= currencyValue;
changeDue = Math.round(changeDue * 100) / 100; // To handle floating point precision issues
amountInDrawer -= currencyValue;
amountToReturn += currencyValue;
}
if (amountToReturn > 0) {
changeArray.push([currency, amountToReturn]);
}
}
// If exact change cannot be given
if (changeDue > 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
return { status: "OPEN", change: changeArray };
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Challenge Information:
JavaScript Algorithms and Data Structures Projects - Cash Register