# Need help with a problem (bug?) in my solution for the Cash Register Java Script Project

Hello,

I have a weird problem with “JavaScript Algorithms and Data Structures Projects: Cash Register”. I think my solution should work, but it doesn’t because of something that I do not know why it happens, which is that the for and while loops somehow mess up the values of “sumInDrawer” and “changeDue” to be a tiny amount smaller than they should be.

If I run the code like this (in the freeCodeCamp UI) then “sumInDrawer” is 335.40999999999997, where is should be 335.41. Similarly with “changeDue”, where a rest of 0.009999999999994869 remains, hindering my function from adding another 0.01 from “cid” to “change.change” (arrays), thus making the function think it has not enough change.

I commented out a workaround I tried which was to round these variables, but they get messes up too often and the rounding adds up and changes the result.

My solution is not very elegant, but looking at the console outputs it should work I think.

Am I doing something wrong or is this a bug? How do I fix this?

The code:

``````function checkCashRegister(price, cash, cid) {

var change = {
status: "OPEN",
change: [
["ONE HUNDRED", 0],
["TWENTY", 0],
["TEN", 0],
["FIVE", 0],
["ONE", 0],
["QUARTER", 0],
["DIME", 0],
["NICKEL", 0],
["PENNY", 0]
]
};

console.log("price is " + price);
console.log("cash is " + cash);

var changeDue = cash - price;
console.log("changeDue is " + changeDue);

var sumInDrawer = 0;
for (var i = 0; i < cid.length; i++) {
sumInDrawer += cid[i][1]
};
//sumInDrawer = Math.round(sumInDrawer * 100) / 100;
console.log("sumInDrawer is " + sumInDrawer);

//for the case of when cid is not enough
if (changeDue > sumInDrawer) {
change.status = "INSUFFICIENT_FUNDS";
change.change = [];
return change
}

//for the case of when the cid is exactly enough
if (changeDue === sumInDrawer) {
change.status = "CLOSED";
change.change = cid;
return change
};

//moving cash from cid to change
while (changeDue >= 100 && cid[8][1] > 0) {
change.change[0][1] += 100;
cid[8][1] -= 100;
changeDue -= 100;
}
while (changeDue >= 20 && cid[7][1] > 0) {
change.change[1][1] += 20;
cid[7][1] -= 20;
changeDue -= 20;
}
while (changeDue >= 10 && cid[6][1] > 0) {
change.change[2][1] += 10;
cid[6][1] -= 10;
changeDue -= 10;
}
while (changeDue >= 5 && cid[5][1] > 0) {
change.change[3][1] += 5;
cid[5][1] -= 5;
changeDue -= 5;
}
while (changeDue >= 1 && cid[4][1] > 0) {
change.change[4][1] += 1;
cid[4][1] -= 1;
changeDue -= 1;
}
while (changeDue >= 0.5 && cid[3][1] > 0) {
change.change[5][1] += 0.5;
cid[3][1] -= 0.5;
changeDue -= 0.5;
}
while (changeDue >= 0.25 && cid[2][1] > 0) {
change.change[6][1] += 0.25;
cid[2][1] -= 0.25;
changeDue -= 0.25;
}
while (changeDue >= 0.1 && cid[1][1] > 0) {
change.change[7][1] += 0.1;
cid[1][1] -= 0.1;
changeDue -= 0.1;
}
while (changeDue >= 0.01 && cid[0][1] > 0) {
change.change[8][1] += 0.01;
cid[0][1] -= 0.01;
changeDue -= 0.01;
}

console.log("After trying to find fitting cash changeDue is " + changeDue);
console.log(change.change);

//for the case of not having the fitting cash
if (changeDue !== 0) {
change.status = "INSUFFICIENT_FUNDS";
change.change = [];
return change
}

//removing empty elements of the change array
for (var i = 8; i >= 0; i--) {
if (change.change[i][1] === 0) {
console.log(change.change[i][1]);
change.change.splice(i, 1);
}
}

//returning the change object
console.log(change.change);
return 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(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]])
``````