Cash register challenge issue with Array

Please guys, desperately need your help. If think basically this code structure is quite similar to the solution code. I actually understand how this can be solved, but there’s one issue I can just not understand. Have already tried to debug, so I know exactly that the issue I’m referring to is happening in line 23 (quite in the middle) in else statement.

if (value[0] === currency[i][0]) { value[1] = value[1] + currency[i][1]; return value; }

The issue: Even I think I never change currency[i][1], it gets changed in line 23 anyway. I don’t understand why. Due to this issue the rest doesn’t work properly. Can someone help?

  **Your code so far**

function checkCashRegister(price, cash, cid) {
let dif = (cash - price)*100, waehrung, objChange = {status: "", change: []};
const currency = [["PENNY", 1], ["NICKEL", 5], ["DIME", 10], ["QUARTER", 25], ["ONE", 100], ["FIVE", 500], ["TEN", 1000], ["TWENTY", 2000], ["ONE HUNDRED", 10000]];
//Check amount of total money in cid
const totalMoney = cid.reduce( (sum, value) => {
  return sum + value[1]*100;
 }, 0);

//Check two of three possible states depending on totalMoney in cid
if (dif === totalMoney) {objChange.status = "CLOSE"; objChange.change = cid;}
else if (totalMoney < dif) {objChange.status = "INSUFFICIENT_FUNDS"; objChange.change = [];}

//Last possible state: Calculate exact change money
for (let i = cid.length-1; i >= 0; i--) {
  while (cid[i][1] > 0 && currency[i][1] <= dif) {
    cid[i][1] = (cid[i][1]*100 - currency[i][1])/100; // Eine Währungseinheit abziehen
    dif = dif-currency[i][1];
    if (objChange.change.find(element => element[0] === currency[i][0]) === undefined) {
      objChange.change.push(currency[i]);
    }
    else {
      objChange.change.forEach(value => {
        if (value[0] === currency[i][0]) { value[1] = value[1] + currency[i][1]; return value; }
      });
    }
  }
}
if (dif === 0 && dif !== totalMoney) {objChange.status = "OPEN";}
}
checkCashRegister(35, 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/89.0.4389.114 Safari/537.36 Edg/89.0.774.76.

Challenge: Cash Register

Link to the challenge:

this you are putting a reference to currency[i]

and here you are working on those same arrays and changing them

try to look at it with a tool like Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code

I see. But don’t really understand why. I even tried avoiding push() and used slice() or other forms (I can’t remember now) but with same result. So what’s the theory behind it? Why does array.prototype.push create a reference if I use an array as the parameter? Can I read about it anywhere?

arrays are passed around as reference
inside currency[i] you have two arrays, you would need to copy the subarrays too

let arr = [1,2,3];
let arr2 = arr;
arr2.push(4);
console.log(arr); // [1, 2, 3, 4]

you can solve this by doing let arr2 = arr.slice() and this will copy the array
but if you have a multidimentsional array it is not so easy

let arr1 = [[1, 2], [3, 4]];
let arr2 = arr1.slice();
arr2.push([5]);
// arr1 was not changed
console.log(arr1); // [[1, 2], [3, 4]]
console.log(arr2); // [[1, 2], [3, 4], [5]]

// but...
arr1[1][0] = 0;
console.log(arr1); // [[1, 2], [0, 4]]
console.log(arr2); // [[1, 2], [0, 4], [5]]

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