I have completed my algorithm but it would not pass. It says it does not pass this test - checkCashRegister(3.26, 100.00, [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.10], [“QUARTER”, 4.25], [“ONE”, 90.00], [“FIVE”, 55.00], [“TEN”, 20.00], [“TWENTY”, 60.00], [“ONE HUNDRED”, 100.00]]) should return [[“TWENTY”, 60.00], [“TEN”, 20.00], [“FIVE”, 15.00], [“ONE”, 1.00], [“QUARTER”, 0.50], [“DIME”, 0.20], [“PENNY”, 0.04]] even though it returns exactly what it is supposed to! Any ideas what is going on? thank you!
Here is my code
const bills = [100, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01];
function checkCashRegister(price, cash, cid) {
let change = cash - price;
let cidTotal = Math.round(cid.reduce(function(acc, curr) {
return acc + curr[1];
}, 0) * 100) / 100;
if (change > cidTotal) {
return "Insufficient Funds";
}
if (change === cidTotal) {
return "Closed";
}
let i = 0;
let moneyBack = cid.reduceRight(function(acc, curr) {
if (curr[1] && change / bills[i] >= 1) {
let item;
if ((change - curr[1]) >= 0) {
item = curr[1];
change = Math.round((change - curr[1]) * 100) / 100;
} else {
item = bills[i] * Math.floor(change / bills[i]);
change = Math.round((change % bills[i]) * 100) / 100;
}
acc.push([curr[0], item]);
}
i++;
return acc;
}, []);
if (change > 0) {
return "Insufficient Funds";
}
return moneyBack;
}
checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);