# JavaScript Algorithms and Data Structures Projects - Cash Register

### Tell us what’s happening:

This code works perfectly fine until pennies get involved within the function, for the given test case here, the pennies in change is showing 0.03 instead of 0.04 to satisfy the change of 96.74 dollars. Can anyone tell me why is this happening

``````function checkCashRegister(price, cash, cid) {
const currency = {"ONE HUNDRED":100, "TWENTY":20, "TEN":10, "FIVE":5, "ONE":1, "QUARTER":0.25, "DIME":0.1, "NICKEL":0.05, "PENNY":0.01};
let changeAmount = cash-price;
let registerTotal = cid.reduce((sum,cur) => sum+=cur[1],0);
let registerStatus = {status: "CLOSED", change:cid}

if(registerTotal<changeAmount) {
registerStatus.status = "INSUFFICIENT_FUNDS";
registerStatus.change = [];
}

else if(registerTotal>changeAmount) {
let changeArr = cid
.reverse()
.reduce((change,cur) => {
let pushValue=0;
while(changeAmount>=currency[cur[0]]&&cur[1]!=0) {
changeAmount -= currency[cur[0]];
cur[1] -= currency[cur[0]];
pushValue += currency[cur[0]];
}
if(pushValue!=0) {
change.push([cur[0],pushValue])
}
return change
} ,[])
registerStatus.status = "OPEN";
registerStatus.change = changeArr;
}
console.log(registerTotal);
return registerStatus;
}

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]]));
``````

User Agent is: `Mozilla/5.0 (X11; CrOS x86_64 14092.77.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.107 Safari/537.36`

### Challenge Information:

JavaScript Algorithms and Data Structures Projects - Cash Register

Got the same problem, neat code, mine is much larger

Hello;

Your code is not perfectly fine. Nothing is perfectly fine; everything is permitted . First, you don’t need to use an anonymous new array for your `checkCashRegister` function. It can take the `cid` array and not change the `cid` and `price` variables. If this is just the test of calculation, it is fine then.

Also, you are making each change calculation one by one to extract from the related drawer change (`cur[1]`) and the remaining payment (`changeAmount`). You are pushing the value to the `currency` array (for printing to the screen, I guess), but it is a long and expensive way; you can do it all at once. And why is everything happening in the reduce function? You already know sum of related change, it is not giving just with amount.

Additionally, try to fix the calculation of the `changeAmount` to two decimal places (`toFixed(2)`). Without fixing it, JS can cause an error with small changes (decimal numbers). Also, this might be needed in your one-by-one change payment loop too. Your bug is probably related with this but still you should print on console and address to origin of your bug. Unfortunately, other bugs and problems cannot be found without more context. If you explain and send your related code, others can try debugging it. What I say is just about your related code block.

@danaagren, welcome to the forum! If you open a new topic with your code and an explanation of your issues, someone can take care of it asap.

Happy coding.

i solved it by converting all to cent, multiply by 100. Now i only got the last three to figure out