JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Hi guys! Well, I’m in this last challenge of JS and I’m in a situation i didn’t expect. With the code you can see I’m having troubles with the pennies. When I operate with the variables I lose a bit of information, seems like a memory issue. When I make that “change-=bills[i];” on my first iteration, instead of getting 56.74 (expected value) I get 56.739999999999995. I lose this bit every iteration, so when I get to the end I have 0.009999999999994869 instead of 0.01, so all my results are lacking one penny. How could I fix this?

  **Your code so far**
function checkCashRegister(price, cash, cid) {
let change=cash-price;
let bills=[0.01 , 0.05 , 0.1 , 0.25 , 1 , 5 , 10 , 20 , 100]
let changeArr=[];
for (let i=cid.length-1; i>=0; i--){
  let money=0;
  while (change>=bills[i]&&cid[i][1]>0){
  if (money >0){
if (changeArr==[]){
  return {status: "INSUFFICIENT_FUNDS", change: []}

return {status: 'OPEN', change: changeArr}
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/ Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Avoid working with decimals, work only with whole numbers (whole number of pennies)

Try wathing this for more explanation

The easiest solution is to add a 0.000001 somewhere as a “fudge factor.” I’m pretty sure this is exactly what I did when I ran into this issue.

Probably a better solution would be to avoid floats/decimals all together, at least until the end where you have to output your result.

I ended up multiplying*100 and then using Math.trunc to remove decimals beyond the second one and then dividing /100 again. Worked fine