Tell us what’s happening:
On 5 of the 6 test cases, I pass. My result only includes the change necessary to display (if there are 0 of a denomination, I don’t add it to the result). However, the final test case, I fail for some reason.
I indicate that the change is 5 pennies, and that the status of the cash register is closed, yet the case still fails.
Your code so far
function checkCashRegister(price, cash, cid) {
let change = cash - price;
let denomIndex = 0;
let result = {status: "", change: []};
let values = [.01, .05, .10, .25, 1, 5, 10, 20, 100];
for (let i = 8; i >= 0; i--) {
// case: check if we can give change for this value (change exceeds the value)
if (!(change >= values[i] && cid[i][1] > 0)) continue;
result.change[denomIndex] = [cid[i][0], 0];
while (change >= values[i] && cid[i][1] > 0) {
// console.log(change, values[i], cid[i][1]);
change = Math.round((change - values[i])*100)/100;
cid[i][1] = Math.round((cid[i][1] - values[i])*100)/100;
// change -= values[i];
// create the entry in change
result.change[denomIndex][1] = Math.round((result.change[denomIndex][1] + values[i])*100)/100;
// console.log(result.change)
}
denomIndex++;
}
if (change > 0) return {status: "INSUFFICIENT_FUNDS", change: []};
let remaining = false;
for (const denom of cid) {
if (denom[1]) remaining = true;
break;
}
result.status = (remaining) ? "OPEN" : "CLOSED";
console.log(result);
return result;
}
// 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]]);
// checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: