function checkCashRegister(price, cash, cid) {
let payBackAmount = cash - price;
let tempArr = cid.slice();
for (let i = 0; i < tempArr.length; i++) {
tempArr[i][1] = 0;
}
console.log("cid is", cid);
}
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],
]);
Here I am trying to copy an array from the parameterized function. cid
is the array which I am trying to pass inside the function and I am trying to copy it into tempArr
. Later when modifying the values of tempArr
, the values of cid
is changing as well.
I have also tried copying the values using let tempArr=[...cid]
and let tempArr=cid.slice(0)