Tell us what’s happening:
hi there! Im having a problem passing one of the provided test cases, even though the console.log() shows, what seems to be, the exact return object as shown in the FCC examples.
test input: 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]])
expected output: {status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}
my console output: {status: “OPEN”, change: Array[7]->7xArray[2]]}, then when I look at the values inside the inner array they seem to be the same as in the expected output…
I googled and googled and all I could find were people having issues after forgetting to round up the change.
Your code so far
var moniez = [
{ denom: "PENNY", value: 0.01 },
{ denom: "NICKEL", value: 0.05 },
{ denom: "DIME", value: 0.10 },
{ denom: "QUARTER", value: 0.25 },
{ denom: "ONE", value: 1.00 },
{ denom: "FIVE", value: 5.00 },
{ denom: "TEN", value: 10.00 },
{ denom: "TWENTY", value: 20.00 },
{ denom: "ONE HUNDRED", value: 100.00 }
];
function checkCashRegister(price, cash, cid) {
var change = cash - price;
// total cash in the drawer
var cid_tot = Number(cid.reduce((a, c) => { a += c[1]; return a }, 0.0).toFixed(2));
if (cid_tot < change) {
return {status: "INSUFFICIENT_FUNDS", change: []};
}
else if (cid_tot === change) {
return {status: "CLOSED", change: cid};
}
cid = cid.reverse();
moniez = moniez.reverse();
var ans = moniez.reduce((agg, curr, idx) => {
if (change >= curr.value) {
let tot_paid = 0;
while (change >= curr.value && cid[idx][1] > 0) {
tot_paid += curr.value;
change -= curr.value;
cid[idx][1] -= curr.value;
change = Math.round(change * 100)/100;
}
agg.push([curr.denom, tot_paid]);
return agg;
}
else {
return agg;
}
}, []);
return ans.length > 0 && change === 0 ? {status: "OPEN", change: ans} : {status: "INSUFFICIENT_FUNDS", change: []};
}
// 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(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]]) ;
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 Firefox/52.0
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/