Cash Register output test not working?

Hi,

Eventhough it seems that my out is correct, I can’t get the test pass. Can someone tell me what is happening here?

let currency={
  "PENNY":0.01,
  "NICKEL":0.05,
  "DIME":0.1,
  "QUARTER":0.25,
  "DOLLAR":1,
  "ONE":1,
  "FIVE": 5,
  "TEN":10,
  "TWENTY":20,
  "ONE HUNDRED":100
};
//Function which return the total value in the CID.
function totalInCashRegister(cid){
  return Math.round(cid.reduce((acc,x)=>{return acc+x[1]},0)*100)/100;
}

//function that giving a cahnge valie and an element of a CID return how ,uch from that notebank you can give to the customer and the left change
function emptyDrawer(change,array){
  let valueNote=currency[array[0]];
  let numberCoins=array[1]/valueNote;
  let returnChange=0;

  while(numberCoins>0 && change>=valueNote){
    change=Math.round((change-valueNote)*100)/100;
    numberCoins=Math.round((numberCoins-1)*100)/100;
    returnChange=returnChange+valueNote; 
  }
  if(returnChange>0){
  return [[[array[0],Math.round(returnChange*100)/100]],change];
  }
  else{
    return [[null],change ]
  }
}
//function taking the whole CID and return the cash and the  left change.
function returnCashToCustomer(change,cid){
  return cid.reverse().reduce((acc,x)=>{
    let emptying=emptyDrawer(acc[1],x);
    if(emptying[1]!=acc[1]){
      acc[1]=emptying[1];
      acc[0].push([emptying[0]]);
    }
    return acc;
    
  },[[],change])
}


function checkCashRegister(price, cash, cid) {
  var change=cash-price;
  var output = { status: null, change: [] };

  // Handle exact change
  if (totalInCashRegister(cid) === change) {
    output.status = 'CLOSED';
    output.change = cid;
    return output;
  }

  // Handle  insufficient funds
  if (totalInCashRegister(cid) < change) {
    output.status = 'INSUFFICIENT_FUNDS';
    output.change = [];
    return output;
  }

  //handling other case
  let cashToReturn=returnCashToCustomer(change,cid);
  //console.log(cashToReturn);
  if (cashToReturn[1]>0){
    output.status = 'INSUFFICIENT_FUNDS';
    output.change = [];
    return output;  

  }
  //we can return all the cash to the custumer
  output.status="OPEN";
  output.change=cashToReturn[0].slice(0);
  return output;
}

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





let test1=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]]);

console.log("-----------------------------")
console.log(test1.status);
console.log(test1.change);
console.log(test1.change.length);

console.log("-----------------------------")

let test2=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]]) ;

console.log("-----------------------------")
console.log(test2.status);
console.log(test2.change);
console.log(test2.change.length);

console.log("-----------------------------")


Best

Raphael

At a glance it appears that you are calculating the correct values but your output object in some cases is different than expected. In the special cases of INSUFFICIENT FUNDS and CLOSED the change is returned correctly but in the case OPEN the value of each individual denomination of monies is returned in a nested array.

[[["TWENTY", 60]], [["TEN", 20]], [["FIVE", 15]], [["ONE", 1]], [["QUARTER", 0.5]], [["DIME", 0.2]], [["PENNY", 0.04]]]

vs

[["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]

You have an extra level of nesting

Thank you a lot. I will try to have a look at it And find a fix. I have still one uestion. How did you find that this extra nesting was my mistake? Are you using sone other tool that display properly the array?

You’re very welcome.

I just copied your code into Developer Tools console of my browser. Then I ran the test cases like this

console.log(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]]))

to log the output. There I could see that your values had an extra level of nesting.

Thank you for letting me know this trick!