Cash Register Project can't see the error

Good morning,
I wonder why my solution doesn’t pass, since (apparently) it gives the expected results. Here is my code:

const coinTable = [
  ["PENNY", .01],
  ["NICKEL", .05],
  ["DIME", .1],
  ["QUARTER", .25],
  ["ONE", 1],
  ["FIVE", 5],
  ["TEN", 10],
  ["TWENTY", 20],
  ["ONE HUNDRED", 100]
];

const emptyCid = [
  ["PENNY", 0],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0]
];

// Counts money in cash register
function verifyFunds ( cashRegister ) {
  let total = 0;
  for (let i = 0; i < cashRegister.length; i++) {
    total = parseFloat( (total + cashRegister[i][1]).toFixed(2) );
  }
  return total;
}

function checkCashRegister(price, cash, cid) {
  let change = {status: undefined, change: []};
  let clientChange = cash - price;
  let funds = verifyFunds(cid);
  let tmp = emptyCid.slice();

  // If doesn't have enough funds
  if ( clientChange > funds ) {
    change.status = "INSUFFICIENT_FUNDS";
    return change;
  } else if ( clientChange == funds ) {
    change.status = "CLOSED";
    change.change = cid.slice();
    return change;
  }

  change.status = "OPEN";
  for (let i = cid.length-1; i >= 0 && clientChange > 0; i-- ) {
    if ( clientChange < coinTable[i][1] || cid[i][1] == 0 ) {
      continue;
    }

    tmp[i][1] = parseFloat( (tmp[i][1] + coinTable[i][1]).toFixed(2) );
    cid[i][1] = parseFloat( (cid[i][1] - coinTable[i][1]).toFixed(2) );
    clientChange = parseFloat( (clientChange - coinTable[i][1]).toFixed(2) );
    i++;
  }
  
  // If can't return exact change
  if ( clientChange > 0 ) {
    change.status = "INSUFFICIENT_FUNDS";
    return change;
  }

  for (let i = tmp.length-1; i >= 0; i-- ) {
    if (tmp[i][1] != 0) change.change.push(tmp[i]);
  }

  return change;
}

It won’t pass in these tests:

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]])

should return

{status: "OPEN", change: [["QUARTER", 0.5]]}.

and

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]])

should return

{status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.

please, when you ask for help, include the challenge/project link

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 previous 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
1 Like

Thanks for the help.