Hey guys. I’m doing the last project for the JS certificate. Can’t see to pass two tests. Damn floating points. I need perspective. What am I missing?
function checkCashRegister(price, cash, cid) {
let statusArray = ["INSUFFICIENT_FUNDS", "CLOSED", "OPEN"];
let currency = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let change = cash - price;
let totalCash = 0;
for(let i = 0; i < cid.length; i++) {
totalCash += (cid[i][1]) * 100;
}
totalCash = totalCash / 100;
let cashInRegister = [...cid];
let receipt = {
status: "",
change : []
};
if(change > totalCash) {
receipt.status = statusArray[0];
receipt.change = [];
} else if(change == totalCash) {
receipt.status = statusArray[1];
receipt.change = [...cashInRegister];
} else {
let matchesGreaterThanChange = [];
for(let i = cashInRegister.length - 1; i >= 0; i--) {
if(change > cashInRegister[i][1] && cashInRegister[i][1] != 0) {
console.log(cashInRegister[i][1]);
console.log(change);
receipt.change.push(cashInRegister[i][0]);
change -= currency[i];
cashInRegister[i][1] -= currency[i];
matchesGreaterThanChange.push(cashInRegister[i][1]);
}
}
if(change > matchesGreaterThanChange[matchesGreaterThanChange.length - 1]) {
receipt.status = statusArray[0];
receipt.change = [];
} else {
receipt.status = statusArray[2];
}
}
console.log(totalCash);
console.log("Change: " + change);
console.log(receipt.status + " and " + receipt.change);
// Here is your change, ma'am.
return receipt;
}
// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);