I need help for the cash register Javascript problem!

Hello! Im having trouble with this problem and I don’t know why. It seems that sometimes the acc stops just before it should, just by one penny. I have also seen that out of nowhere, very small decimals appear and mess up the code. Here is what I wrote. If someone knows what is happening, please let me know!

function checkCashRegister(price, cash, cid) {
  // Variables declarations
  let answer = [];
  const change = cash - price;
  let acc = 0;
  cid.reverse(); // This is so the for loop starts with the biggest currencies first
  const values = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01] 

  // Creating a function that iterates through the current currency until needed.Stops if the amount hits 0. Returns the amount left of it. 

  function addToChange(c, value){
    let amount = c / value;
    while(acc + value <= change && amount > 0){
      acc += value;
      c -= value;
      amount -= 1;
    }
    return c;
  }

  // Applying the function to every currency and updating the values of the variables

  for(let i = 0; i < cid.length; i++){
    let cAfter = addToChange(cid[i][1], values[i]);
    answer.push([cid[i][0], Number((cid[i][1] - cAfter).toFixed(2))]);
    cid[i][1] = cAfter;
    if(acc === change) break;
  }

  answer = answer.filter(c => c[1] !== 0);

 // Return declarations

  if(acc === change){
    return {
      status: 'OPEN',
      change: answer
    }
  } else if(acc !== change){
    return {
      status: 'INSUFFICIENT_FUNDS',
      change: []
    }
  } 
  
  // Ignore this part

  else if(cid.every(c => c[1] === 0)){
      return {
        status: 'CLOSED',
        change: cid
      }
  }

}

console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I suggest this video on the topic: