Hi!
I am trying to resolve the algorithm challenge named " Cash Register". Everything is working fine, except that one user story is failing. The test that fails is 3. I find [“PENNY”, 0.03] instead of [“PENNY”, 0.04]. My solution is below:
let change = cash - price;
let test = cash - price;
let total = 0;
let remaining = 0;
const output = [];
const cidReverse = cid.reverse();
const currencies = [
{name: 'ONE HUNDRED', amount: 100},
{name: 'TWENTY', amount: 20},
{name: 'TEN', amount: 10},
{name: 'FIVE', amount: 5},
{name: 'ONE', amount: 1},
{name: 'QUARTER', amount: 0.25},
{name: 'DIME', amount: 0.1},
{name: 'NICKEL', amount: 0.05},
{name: 'PENNY', amount: 0.01}
];
const currencyInfo = (currency) => currencies.find(curr => curr.name === currency);
cidReverse.forEach(currArr => {
const currencyValue = currencyInfo(currArr[0]).amount;
const temp = []
remaining += currArr[1]
if (change >= currencyValue || change >= currArr[1]) {
temp.push(currArr[0]);
const possibleBills = Math.floor(change / currencyValue) * currencyValue;
if (change > 0) {
if (possibleBills > currArr[1]) {
temp.push(currArr[1])
change = (possibleBills - currArr[1]) + (change % currencyValue)
} else {
temp.push(possibleBills)
change = change % currencyValue
}
}
total += temp[1]
output.push(temp)
}
})
let status = "OPEN";
let change1 = output;
if (remaining === test) {
status = "CLOSED";
change1 = change1.reverse();
}
if (total !== (cash - price)) {
status = "INSUFFICIENT_FUNDS";
// change1 = [];
}
return {
status,
change: change1
}
Can I get help on how to fix my algo logic?