# Cash Register Failing Test Case Despite Seemingly Correct Result?

**URL:** <https://forum.freecodecamp.org/t/cash-register-failing-test-case-despite-seemingly-correct-result/271483>\
**Category:** General\
**Created:** [April 14, 2019, 9:42pm UTC](https://forum.freecodecamp.org/t/cash-register-failing-test-case-despite-seemingly-correct-result/271483 "2019-04-14T21:42:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![glyn](https://avatars.discourse-cdn.com/v4/letter/g/b9bd4f/32.png) [@glyn](https://forum.freecodecamp.org/u/glyn)\
**Post date:** [April 14, 2019, 9:42pm UTC](https://forum.freecodecamp.org/t/cash-register-failing-test-case-despite-seemingly-correct-result/271483/1 "2019-04-14T21:42:07Z")

</div>

Hi…  
I am a bit at a loss here. This is for the cash register exercise for the final JS project. In the interpreter that I am using, my code passes all test cases. Yet, when I run it through fcc’s test cases, it gives me a fail for one test and one test only - 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]]);

When I run this call with my code using a nodejs interpreter, I get the result  
{ status: ‘OPEN’, change:  
[[ ‘TWENTY’, 60], [‘TEN’, 20], [‘FIVE’, 15], [‘ONE’, 1], [‘QUARTER’, 0.5], [‘DIME’, 0.2], [‘PENNY’, 0.04] ] }

The test case says I should get the result:  
{status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}

What is going wrong here? Thanks.

EDIT: Added some comments to the code to be clearer.

```auto

function copyArray(array){ // copies the array so I am not using same object
  let newarray = [];
  for (let subarray of array){
    let inarray = [];
    for (let subitem of subarray){
      inarray.push(subitem);
    }
    newarray.push(inarray);
  }
  return newarray;
}

function sumRegister(cid){ // sums amt of money in register (to identify closed vs. open)
  let sum = 0;
  for (let i = 0; i < cid.length; i+=1){
    sum += cid[i][1]*1000;
  }
  return sum/1000;
}

function checkCashRegister(price, cash, cid) {
  let denoms = [1, 5, 10, 25, 100, 500, 1000, 2000, 10000];
  let change = (cash-price)*100; // change required
  let newcid = copyArray(cid); // second copy of cash in drawer
  for (let item of newcid){
    item[1] = item[1]*100; // multiply to avoid floats
  }
  for (let index = denoms.length-1; index >= 0; index-=1){ // iterate through denominations
    let denom = denoms[index];
    let amt = Math.floor(change/denom); // find num of times denomination can be used
    for (let i = 1; i <= amt; i+=1){ // subtract money from (new) change in drawer
      if (change == 0){
        break;
      }
      else if(newcid[index][1] - denom >= 0){
        newcid[index][1] = newcid[index][1]-denom;
        change -= denom;
      }
    }
    if (change == 0 && sumRegister(cid)*100 == (cash-price)*100){ 
      return {'status':'CLOSED', 'change': cid}; // if money in register = money used for change, "Closed"
    }
    else if (change == 0){
      for (let i = 0; i < newcid.length; i+=1){ // finds difference between cid and amounts taken
        newcid[i][1] = Math.round(cid[i][1]*100-newcid[i][1])/100;
      }
      let newercid = [];
      for (let item of newcid){
        if (item[1] > 0){ // only puts items where change was taken from
          newercid.push(item);
        }
      }
      newercid.sort(function(item){return item[1]}); // sorts drawer from highest to smallest
      return {'status': "OPEN", 'change': newercid};
    }
  }
  return {'status': "INSUFFICIENT_FUNDS", 'change': []};
}

// 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(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]]);

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36`.

---

<div class="post-metadata">

**Author:** ![s-projects18](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/s-projects18/32/106502_2.png) [@s-projects18](https://forum.freecodecamp.org/u/s-projects18)\
**Post date:** [April 15, 2019, 5:04am UTC](https://forum.freecodecamp.org/t/cash-register-failing-test-case-despite-seemingly-correct-result/271483/2 "2019-04-15T05:04:09Z")

</div>

Well, I didn’t dig deep, but to me it seems the result ordner (in FCC) is different (console):

```auto
1. 0: (2) ["PENNY", 0.04]
2. 1: (2) ["DIME", 0.2]
3. 2: (2) ["QUARTER", 0.5]
4. 3: (2) ["ONE", 1]
5. 4: (2) ["FIVE", 15]
6. 5: (2) ["TEN", 20]
7. 6: (2) ["TWENTY", 60]

```

---

<div class="post-metadata">

**Author:** ![glyn](https://avatars.discourse-cdn.com/v4/letter/g/b9bd4f/32.png) [@glyn](https://forum.freecodecamp.org/u/glyn)\
**Post date:** [April 15, 2019, 5:54am UTC](https://forum.freecodecamp.org/t/cash-register-failing-test-case-despite-seemingly-correct-result/271483/3 "2019-04-15T05:54:44Z")

</div>

Thank you so much! It’s completely bizarre to me, because in repl.it it returns the correct order, but you are correct, in the console it does not! I added a line to reverse my array and BAM passes all tests. 🙂

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:03pm UTC](https://forum.freecodecamp.org/t/cash-register-failing-test-case-despite-seemingly-correct-result/271483/4 "2021-01-17T20:03:04Z")

</div>

Closed on January 17, 2021, 8:03pm UTC


