# JavaScript Algorithms and Data Structures Projects - Cash Register

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:

This is because you are not handling the case where you have enough money in the cash register but not enough of a certain coin to return the exact change. You would have to use a combination of coins to give the change.

To do this, you can add another loop inside the existing for loop to check for the exact change. For each iteration, you can keep decrementing the amount by a smaller denomination until you reach 0

1 Like

thanks, I solved it. I founded that one loop is enought but the real bug is the floating point calculations.

by searching on stackoverflow I found that `Math.round(remains * 100) / 100;` really solved the problem

I also refactored some things like normal for loop became a foreach (because for me was more readible), object variable now became some cost variables.

``````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: "CLOSED", change: cid };

let result;
let remains = TOTAL_CHANGE_TO_GIVE;

cid.reverse().forEach(([key, value]) => {
const sizeOfOneCoin = DEFAULT_DATA[key];
const nCoins = Math.round(value / sizeOfOneCoin);
const maxCoinsWeCanGive = Math.min(Math.floor(remains / sizeOfOneCoin), nCoins);
const chargeOneTypeOfCoin = maxCoinsWeCanGive * sizeOfOneCoin;

if(maxCoinsWeCanGive === 0) return;

remains -= chargeOneTypeOfCoin;
remains = Math.round(remains * 100) / 100; // solving that annoying 0.0099999999999998 floating bug
result = result || [];
result.push([key, chargeOneTypeOfCoin]);
})

if (remains > 0) return { status: "INSUFFICIENT_FUNDS", change: [] };

return { status: "OPEN", change: result,};
}
``````

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.