Slice not working for nested arrays? How can I avoid mutilation

I can’t figure out how to stop mutilating my val and my cid arrays. I have tried to slice them into separate variables a thousand different ways. What’s the proper way of avoiding mutilation of my arrays?

  **Your code so far**

function checkCashRegister(price, cash, cid) {

var val = [];
val = [["PENNY", .01],
["NICKEL", .05],
["DIME", .1],
["QUARTER", .25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["HUNDRED", 100]];

const calcQuant = (arr) => {
  for (let i in val) {
    arr[i][1] = arr[i][1] / (val[i][1] * 100);
    arr[i][1] = arr[i][1].toFixed(2) * 100;
  }
  return arr;
}
const calcVal = (arr) => {
  for (let i in val) {
    arr[i][1] = arr[i][1] * (val[i][1] * 100);
    arr[i][1] = arr[i][1].toFixed(2) / 100;
  }
  return arr;
}
let j = 8
let owed = (cash - price).toFixed(2);
do {
  change += calcQuant(cid[j][1])
  owed -= calcVal(cid[j][1])
  if (owed > calcVal()) {
    break;
  }
  else {
    j--;
    if (j === 0 || owed <= 0)
      return change;
  }
break;
}
while (owed > val[j][1]);
console.log(change)

var change;
return change;
}

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]]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Cash Register

Link to the challenge:

I’m not sure I understand what’s the problem. Could you give and example of what is (incorrectly) happening and what would you want to happen instead?

Yeah, so I’m trying to avoid mutilation of my ‘val’ and ‘cid’. It seems they’re being mutilated somewhere along the code because some of the outputs that I’m getting are really absurd. I normally would create a clone of the array by using the slice method but all that returns is an empty array.

var x = val.slice() // []
var y = cid.slice // []

Thanks for your swift reply!

Edit: Okay wait. So if I do

var y = cid.slice()

and then run my function calcQuant(y);
console.log(cid) changes and returns the same output that y does.

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