Hi guys, i don’t know if i’m doing something wrong. But i already coded the solution and it passes every test except the last one!!!. Can you please inform me if there’s something i’m missing.
Here’s my code:
function checkCashRegister(price, cash, cid) {
let change = cash - price;
let currencies =
{
"ONE HUNDRED": 100,
TWENTY: 20,
TEN: 10,
FIVE: 5,
ONE: 1,
QUARTER: 0.25,
DIME: 0.1,
NICKEL: 0.05,
PENNY: 0.01
}
let result;
let cashInDrawerSum = cid.reduce((cashInDrawerSum, item) => {
return cashInDrawerSum + item[1];
}, 0);
let cidCopy = JSON.parse(JSON.stringify(cid));
let cashInDrawer = Object.fromEntries(cidCopy.reverse());
if(cashInDrawerSum < change)
{
result = {status: "INSUFFICIENT_FUNDS", change: []};
}
else if(cashInDrawerSum == change)
{
result = {status: "CLOSED", change: [cid]};
}
else
{
let changeArray = [];
let untillChange = 0;
for(let currency in currencies)
{
let currencySum = 0;
if(typeof cashInDrawer[currency] !== 'undefined' && (currencies[currency] <= change) && ((untillChange + currencies[currency]) <= change))
{
let timesFound = cashInDrawer[currency] / currencies[currency];
for(let i = 0; i < timesFound; i++)
{
if((currencySum + untillChange) < change)
{
currencySum += currencies[currency];
if((currencySum + currencies[currency] + untillChange).toFixed(2) > change)
{
i = timesFound;
}
}
}
untillChange += currencySum;
untillChange = parseFloat(untillChange.toFixed(2))
changeArray.push([currency, currencySum]);
}
}
result = (untillChange == change) ? {status: "OPEN", change: changeArray} : {status: "INSUFFICIENT_FUNDS", change: []};
}
return result;
}