Cash register help

Hello there. I have a problem passing the last test in javaScript Cash Register challange. I do not know what I am doing wrong, because test showing me that the resolution is exactly like mine, but test fails.

function checkCashRegister(price, cash, cid) {
   //setup
   /*const table = [
    ["PENNY", 0.01],
	  ["NICKEL", 0.05],
    ["DIME", 0.1],
    ["QUARTER", 0.25],
    ["ONE", 1],
    ["FIVE", 5],
    ["TEN", 10],
    ["TWENTY", 20],
    ["ONE HUNDRED", 100]
  ];*/
  const table = [
    ["ONE HUNDRED", "TWENTY", "TEN", "FIVE", "ONE", "QUARTER", "DIME", "NICKEL", "PENNY"],
    [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01]
  ];
  const availableCash = [...cid].reverse();
  const change = [
    [ "ONE HUNDRED", 0 ],
    [ "TWENTY", 0 ],
    [ "TEN", 0 ],
    [ "FIVE", 0 ],
    [ "ONE", 0 ],
    [ "QUARTER", 0 ],
    [ "DIME", 0 ],
    [ "NICKEL", 0 ],
    [ "PENNY", 0 ]
  ];
  const currencify = (expression, precision = 2) => +(expression.toFixed(precision));
  let rest = currencify(cash - price);
  console.log("\"rest\" before operations:", rest, "\n\"availableCash\" before operations:\n", availableCash, "\n");
  //end of setup
  function shift(cashForOperation, position, amountOfMoney, nameOfMoney, toChange) {
    if (cashForOperation[position][0] == nameOfMoney && cashForOperation[position][1] >= amountOfMoney && rest >= amountOfMoney) {
      cashForOperation[position][1] = currencify(cashForOperation[position][1] - amountOfMoney);
      rest = currencify(rest - amountOfMoney);
      for (let j = 0; j < toChange.length; j++) {
        if (toChange[j][0] == nameOfMoney) { 
          toChange[j][1] = currencify(toChange[j][1] + amountOfMoney);
        }
      }
      shift(cashForOperation, position, amountOfMoney, nameOfMoney, toChange);
    }
  };
  for (let i = 0; i < availableCash.length; i++) {
    let f = availableCash[i][0]; //nameOfMoney, should be "ONE HUNDRED", then "TWENTY" etc.
    let g = table[1][table[0].indexOf(f)]; //amountOfMoney, should be 100, then 20 etc.
    shift(availableCash, i, g, f, change); //should be shift(0, 100, "ONE HUNDRED"), then shift(1, 20, "TWENTY") etc.
    console.log(i, g, f)
  }
  console.log("\n\"rest\" after operations:", rest, "\n\"availableCash\" after operations:\n", availableCash, "\n");
  if (rest > 0) {
    console.log("return:\n", {status: "INSUFFICIENT_FUNDS", change: []});
    return {status: "INSUFFICIENT_FUNDS", change: []};
  }
  let literralyAvailableCash = 0;
  for (let i = 0; i < change.length; i++) {
    literralyAvailableCash = currencify(literralyAvailableCash + availableCash[i][1]);
  }
  if (literralyAvailableCash == 0) {
    console.log("return:\n", {status: "CLOSED", change: change.reverse()})
    return {status: "CLOSED", change: change.reverse()}
    }
  console.log("return:\n", {status: "OPEN", change: change.filter(el => el[1] != 0)});
  return  {status: "OPEN", change: change.filter(el => el[1] != 0)};
}
//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]]);
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]]);
console.log("------------------------------------------------------------");
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
console.log("------------------------------------------------------------");
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
console.log("------------------------------------------------------------");
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

console output:

"rest" before operations: 0.5 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 60 ],
  [ 'TEN', 20 ],
  [ 'FIVE', 55 ],
  [ 'ONE', 90 ],
  [ 'QUARTER', 4.25 ],
  [ 'DIME', 3.1 ],
  [ 'NICKEL', 2.05 ],
  [ 'PENNY', 1.01 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 60 ],
  [ 'TEN', 20 ],
  [ 'FIVE', 55 ],
  [ 'ONE', 90 ],
  [ 'QUARTER', 3.75 ],
  [ 'DIME', 3.1 ],
  [ 'NICKEL', 2.05 ],
  [ 'PENNY', 1.01 ] ] 

return:
 { status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }
"rest" before operations: 0.5 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 60 ],
  [ 'TEN', 20 ],
  [ 'FIVE', 55 ],
  [ 'ONE', 90 ],
  [ 'QUARTER', 4.25 ],
  [ 'DIME', 3.1 ],
  [ 'NICKEL', 2.05 ],
  [ 'PENNY', 1.01 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 60 ],
  [ 'TEN', 20 ],
  [ 'FIVE', 55 ],
  [ 'ONE', 90 ],
  [ 'QUARTER', 3.75 ],
  [ 'DIME', 3.1 ],
  [ 'NICKEL', 2.05 ],
  [ 'PENNY', 1.01 ] ] 

return:
 { status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }
------------------------------------------------------------
"rest" before operations: 96.74 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 60 ],
  [ 'TEN', 20 ],
  [ 'FIVE', 55 ],
  [ 'ONE', 90 ],
  [ 'QUARTER', 4.25 ],
  [ 'DIME', 3.1 ],
  [ 'NICKEL', 2.05 ],
  [ 'PENNY', 1.01 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 40 ],
  [ 'ONE', 89 ],
  [ 'QUARTER', 3.75 ],
  [ 'DIME', 2.9 ],
  [ 'NICKEL', 2.05 ],
  [ 'PENNY', 0.97 ] ] 

return:
 { status: 'OPEN',
  change: 
   [ [ 'TWENTY', 60 ],
     [ 'TEN', 20 ],
     [ 'FIVE', 15 ],
     [ 'ONE', 1 ],
     [ 'QUARTER', 0.5 ],
     [ 'DIME', 0.2 ],
     [ 'PENNY', 0.04 ] ] }
------------------------------------------------------------
"rest" before operations: 0.5 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 0 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0.01 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0.49 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 0 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0 ] ] 

return:
 { status: 'INSUFFICIENT_FUNDS', change: [] }
------------------------------------------------------------
"rest" before operations: 0.5 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 1 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0.01 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0.49 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 1 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0 ] ] 

return:
 { status: 'INSUFFICIENT_FUNDS', change: [] }
------------------------------------------------------------
"rest" before operations: 0.5 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 0 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0.5 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 0 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0 ] ] 

return:
 { status: 'CLOSED',
  change: 
   [ [ 'PENNY', 0.5 ],
     [ 'NICKEL', 0 ],
     [ 'DIME', 0 ],
     [ 'QUARTER', 0 ],
     [ 'ONE', 0 ],
     [ 'FIVE', 0 ],
     [ 'TEN', 0 ],
     [ 'TWENTY', 0 ],
     [ 'ONE HUNDRED', 0 ] ] }
"rest" before operations: 0.5 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 60 ],
  [ 'TEN', 20 ],
  [ 'FIVE', 55 ],
  [ 'ONE', 90 ],
  [ 'QUARTER', 4.25 ],
  [ 'DIME', 3.1 ],
  [ 'NICKEL', 2.05 ],
  [ 'PENNY', 1.01 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 60 ],
  [ 'TEN', 20 ],
  [ 'FIVE', 55 ],
  [ 'ONE', 90 ],
  [ 'QUARTER', 3.75 ],
  [ 'DIME', 3.1 ],
  [ 'NICKEL', 2.05 ],
  [ 'PENNY', 1.01 ] ] 

return:
 { status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }
"rest" before operations: 96.74 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 60 ],
  [ 'TEN', 20 ],
  [ 'FIVE', 55 ],
  [ 'ONE', 90 ],
  [ 'QUARTER', 4.25 ],
  [ 'DIME', 3.1 ],
  [ 'NICKEL', 2.05 ],
  [ 'PENNY', 1.01 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 40 ],
  [ 'ONE', 89 ],
  [ 'QUARTER', 3.75 ],
  [ 'DIME', 2.9 ],
  [ 'NICKEL', 2.05 ],
  [ 'PENNY', 0.97 ] ] 

return:
 { status: 'OPEN',
  change: 
   [ [ 'TWENTY', 60 ],
     [ 'TEN', 20 ],
     [ 'FIVE', 15 ],
     [ 'ONE', 1 ],
     [ 'QUARTER', 0.5 ],
     [ 'DIME', 0.2 ],
     [ 'PENNY', 0.04 ] ] }
"rest" before operations: 0.5 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 0 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0.01 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0.49 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 0 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0 ] ] 

return:
 { status: 'INSUFFICIENT_FUNDS', change: [] }
"rest" before operations: 0.5 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 1 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0.01 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0.49 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 1 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0 ] ] 

return:
 { status: 'INSUFFICIENT_FUNDS', change: [] }
"rest" before operations: 0.5 
"availableCash" before operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 0 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0.5 ] ] 

0 100 ONE HUNDRED
1 20 TWENTY
2 10 TEN
3 5 FIVE
4 1 ONE
5 0.25 QUARTER
6 0.1 DIME
7 0.05 NICKEL
8 0.01 PENNY

"rest" after operations: 0 
"availableCash" after operations:
 [ [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 0 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0 ] ] 

return:
 { status: 'CLOSED',
  change: 
   [ [ 'PENNY', 0.5 ],
     [ 'NICKEL', 0 ],
     [ 'DIME', 0 ],
     [ 'QUARTER', 0 ],
     [ 'ONE', 0 ],
     [ 'FIVE', 0 ],
     [ 'TEN', 0 ],
     [ 'TWENTY', 0 ],
     [ 'ONE HUNDRED', 0 ] ] }

Problem solved.
.reverse() is mutating operation

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.