Tell us what’s happening:
I am doing the last project, pretty much finished. but when I tested All them pass but not one.
I am literally trying to figure out for 2 hours, can someone more experienced tell me what i am wrong?
Your code so far
function checkCashRegister(price, cash, cid) {
const DEFAULT_DATA = {
PENNY: 0.01,
NICKEL: 0.05,
DIME: 0.1,
QUARTER: 0.25,
ONE: 1,
FIVE: 5,
TEN: 10,
TWENTY: 20,
"ONE HUNDRED": 100,
};
const TOTAL_CHANGE_TO_GIVE = cash - price;
const TOTAL_COINS_INSIDE = cid.reduce((acc, curr) => acc + curr[1], 0);
if (TOTAL_COINS_INSIDE < TOTAL_CHANGE_TO_GIVE) return { status: "INSUFFICIENT_FUNDS", change: [] };
if (TOTAL_COINS_INSIDE === TOTAL_CHANGE_TO_GIVE) return { status: "CLOSED", change: cid };
return otherCases();
function otherCases() {
let result;
let remains = TOTAL_CHANGE_TO_GIVE;
for (let i = cid.length - 1; i >= 0; i--) {
const entry = cid[i];
const obj = {
key: entry[0],
cashInside: entry[1],
numberOfCoins: Math.round(entry[1] / DEFAULT_DATA[entry[0]]),
};
if (obj.cashInside === 0) continue;
if (obj.cashInside > remains) {
obj.cashInside = Math.floor(remains / DEFAULT_DATA[entry[0]]) * DEFAULT_DATA[entry[0]];
remains -= obj.cashInside;
result = result ? [...result, [obj.key, obj.cashInside]] : [[obj.key, obj.cashInside]];
}
if (remains === 0) break;
}
if (remains > 0) return { status: "INSUFFICIENT_FUNDS", change: [] };
return {
status: "OPEN",
change: result.filter((a) => a[1] > 0),
};
}
}
// the only test that i dont pass
console.log(
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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register
Link to the challenge: