Code works correctly in visual studio but not in freecodecamp

I’m working through the Exact Change challenge.

let values = {
  "PENNY": 0,
  "NICKEL": 0,
  "DIME": 0,
  "QUARTER": 0,
  "ONE": 0,
  "FIVE": 0,
  "TEN": 0,
  "TWENTY": 0,
  "ONE HUNDRED": 0,
  "TOTAL": 0,
};

function checkCashRegister(price, cash, cid) {
  let retCid = [];

  function takeMoney(field, num) {
    change -= num;
    values[field] -= num;
    values.TOTAL -= num;
    retCid.forEach((arr, index) => {
      if (arr[0] == field) {
        retCid[index][1] += num;
      }
    });

    try {
      change = change.toFixed(2);
      values.TOTAL = values.TOTAL.toFixed(2);
    } catch (error) {
      console.log(error);
    }
  }


    cid.forEach((arr) => {
      values[arr[0]] = arr[1];
      values.TOTAL += arr[1];
    });

    values.TOTAL = values.TOTAL.toFixed(2); 

    cid.forEach(num => retCid.push([num[0], 0]));
    
    let change = cash - price;
    console.log(values);

    while(change != 0.00 && values.TOTAL != 0.00) {
            
      if (change >= 100 && values["ONE HUNDRED"]) {
        takeMoney("ONE HUNDRED", 100);
      } else if (change >= 20 && values.TWENTY) {
        takeMoney("TWENTY", 20);
      } else if (change >= 10 && values.TEN) {
        takeMoney("TEN", 10);
      } else if (change >= 5 && values.FIVE) {
        takeMoney("FIVE", 5);
      } else if (change >= 1 && values.ONE) {
        takeMoney("ONE", 1);
      } else if (change >= 0.25 && values.QUARTER) {
        takeMoney("QUARTER", 0.25);
      } else if (change >= 0.1 && values.DIME) {
        takeMoney("DIME", 0.1); 
      } else if (change >= 0.05 && values.NICKEL) {
        takeMoney("NICKEL", 0.05);
      } else if (change >= 0.01 && values.PENNY) {
        takeMoney("PENNY", 0.01);
      } 
    }
    
    if (change > 0.00 && values.TOTAL == 0.00) {
      return "Insufficient Funds";
    } else if (change == 0.00 && values.TOTAL == 0.00) {
      return "Closed";
    } else {
      retCid = retCid.filter(arr => arr[1]);
      return retCid.reverse();
    }
  }

The following code returns the string “Closed”.

  let result = checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
  console.log(result);

But freecodecamp marks it as incorrect. Despite the only criteria being to return a string.

The following three are also shown as incorrect despite working in visual studio. This is really starting to give me a headache.

checkCashRegister(19.50, 20.00, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return “Insufficient Funds”.

checkCashRegister(19.50, 20.00, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 1.00], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return “Insufficient Funds”.

checkCashRegister(19.50, 20.00, [[“PENNY”, 0.50], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return “Closed”.

checkCashRegister(19.50, 20.00, [[“PENNY”, 0.50], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return a string.

Thanks to anyone who can help.

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the new value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

When you fix this, you will still fail one of the tests because the infinite loop protection will trigger.