Cash Register Failing Test Case Despite Seemingly Correct Result?

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.


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.

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

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

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. :slight_smile: