Cash Register test cases #3 and #6 fail

Tell us what’s happening:
I can’t get the last JavaScript project right. I fail test case #3 and #6. #3 is because of floating point inaccuracy and #6 is because I handle two arrays so that I could return the original cid in case of a closed drawer but the array copying does not work the way I would expect. What should I do with these?

Your code so far


function checkCashRegister(price, cash, cid) {
    const coins = {
      PENNY: 0.01,
      NICKEL: 0.05,
      DIME: 0.1,
      QUARTER: 0.25,
      ONE: 1,
      FIVE: 5,
      TEN: 10,
      TWENTY: 20,
      "ONE HUNDRED": 100  
    }

    let answer = {
      status: "",
      change: []
    }

    let change = cash - price;
    let cidCopy = cid;

    for (let i = cid.length - 1; i >= 0; i--) {
      const coin = cidCopy[i][0];
      const value = coins[coin];
      let remainingInDrawer = cidCopy[i][1];
      let changeOfCoin = 0;
      if(remainingInDrawer > 0){
        changeOfCoin = Math.floor(change/value) * value;
      }
      console.log(remainingInDrawer + " " + changeOfCoin + " " + change);
      /*while(change - value >= 0 && cidCopy[i][1]-value >= 0) {
          change -= value;
          cidCopy[i][1] -= value;
          changeOfCoin += value;
      }*/
      if (changeOfCoin > 0) {
        if(remainingInDrawer >= changeOfCoin) {
          answer.change.push([coin,changeOfCoin]);
          cidCopy[i][1] = remainingInDrawer-changeOfCoin;
          change -= changeOfCoin;
        }
        else {
          answer.change.push([coin,remainingInDrawer]);
          cidCopy[i][1] = 0;
          change -= remainingInDrawer;
        }
      }
    }
    if (change > 0) {
      answer.status = "INSUFFICIENT_FUNDS";
      answer.change = [];
      return answer;
      
    }
    else if (change < 0) {
      return "ERROR, NEGATIVE CHANGE";
    }
    else if (cidCopy.some(coin => coin[1] > 0)) {
      answer.status = "OPEN";
      return answer;
    }
    else {
      answer.status = "CLOSED";
      answer.change = cid;
      return answer;
    }

}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

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 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 OPR/60.0.3255.95 (Edition avira-2).

Link to the challenge:

For the floating point error you need to put everything in units (multiply everything by 100) and then do the opposite before returning the values

For copying the array, I would suggest JSON.parse(JSON.stringify(//your array here//)

Because what you did with let arr2 = arr1 you are just creating a reference, not a copy, and as it is a multidimensional object the usual ways (slice, concat, spread operator) would not create copies of the inner objects

Thank you for your help. :slight_smile: