Float Problems on Cash Register challenge

Tell us what’s happening:

Hi

This is my code but it is not finished, I know why the 2 last don’t work, but I can’t really continue because I have float problems. I googled a little and found that it was normal that integer sum were not exact and that I have to user num.toFixed(2);

So I tried, I put toFixed(2) on all floats and nothing worked anymore, so I tried to put only one on my toChange variable which is keeping track of how much money is lacking to complete the change.
On the example I run, the last one of the problem, when I use a console.log to keep track on my toChange at each loop iteration, I see the number of pennies in the draw going down from 0,5, and it stops randomly in the middle, sometimes at 0.29, sometime 0.07 or 0,03 etc and nothing after that.

So I googled again and found some people talking about toFixed working only on chrome and being bugged in others browsers.

So I don’t really get what to do next, is there no safe way to do calculus with float in javascript ?? Do I really have to multiply all my numbers by 100 then divide the result? I hope there is a safe way to do that

Thanks in advance

Your code so far


function checkCashRegister(price, cash, cid) {

  const currencies = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
  let cashInDrawer = cid;
  let stat = "OPEN";
  let toChange = cash - price;
  let change = [];
  let canChange = [];

  function cashLeft() {
    
    let total = 0;
    for (let i = 0; i < cashInDrawer.length; i++) {
        total += cashInDrawer[i][1];
    }
    if (total != 0) {
      return true;
     }
     else {
       return false;
     }
  } //Check if there is money left in the drawer

  for (let i = 0; i < currencies.length; i++) {
    if (currencies[i] < toChange && cashInDrawer[i][1] > 0) {
      canChange.push(currencies[i]);
    }
  } // Build an array with only the coins/bills we can use (bigger values are excluded)

  for (let i = canChange.length - 1; i > -1; i--) {

    let tempChange = 0;

    while(cashInDrawer[i][1] >= canChange[i] && canChange[i] <= toChange ) {
      cashInDrawer[i][1] -= canChange[i];
      toChange -= canChange[i];
      toChange = toChange.toFixed(2);
      tempChange += canChange[i]; 
      console.log(toChange);     
    }

    if (toChange == 0) {

      if(!cashLeft()) {
        stat = "CLOSED";
      }
      change.push([cashInDrawer[i][0], tempChange]);
      console.log(cashInDrawer);
      //console.log({status: stat,change: change.concat(cashInDrawer.filter(item => item[1] == 0))});

      return {status: stat, change: change};
    }

    
    if(!cashLeft()) {
      stat = "INSUFFICIENT_FUNDS";
      return {status: stat, change:[]};
    }

    if (tempChange) {
      change.push([cashInDrawer[i][0], tempChange]);
    }

   // console.log(change);
  }

  return  {status: stat,change: change};

// Example cash-in-drawer array:
 /* let cashInDrawer = [["PENNY", 1.01],
                      ["NICKEL", 2.05],
                      ["DIME", 3.1],
                      ["QUARTER", 4.25],
                      ["ONE", 90],
                      ["FIVE", 55],
                      ["TEN", 20],
                      ["TWENTY", 60],
                      ["HUNDRED", 100]];*/
}
console.clear();
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

You can do calculus with floats in JS, however you need to be careful. Keep in mind that toFixed() returns a string and you may get into trouble because of that. Also, in the while loop, part of the test condition involves cashInDrawer[i][1], not only toChange. So, you probably need to use toFixed on it as well, otherwise it may fail prematurely.