Have I run into a bug?Or is this supposed to happend?

I am doing this challenge and I have run into something at least weird.
I am trying to make a cid clone so it doesn’t change it’s value after being used in my own function called getNewChange()(this function should return the new cid balance after the cashier would return the change):
Here’s a part of the code simplified:

   const newCid = [...cid]; // NOT WORKING.
   const newCidTryTry = cid.slice(); //NOT WORKING
   const newCidTryTryTry = cid.map((e) =>{ return e}) //NOT WORKING


  const getNewChange = (cid,changeValue) =>{
    let cidClone = [...cid];    //NOT WORKING.
    const objChange = {};
    for(let i = 0;i<changeValue.length;i++){
      objChange[changeValue[i][0]] = changeValue[i][1];
    }
    for(let i = 0;i<cidClone.length;i++){
      cidClone[i][1] = (cidClone[i][1] - objChange[cidClone[i][0]]);
      if(isNaN(cidClone[i][1])){
        cidClone[i][1] = 0;
      };
    }
    retrun cidClone;
}

I tried saving my initial value of the cid 4-5 times,and it doesn’t work,after it gets passed to the getNewChange function it changes.

here’s the hole code if you want to have a look for yourself:

const register = {open:"OPEN",insufficient:"INSUFFICIENT_FUNDS",closed:"CLOSED"}

function checkCashRegister(price, cash, cid) {
  const firstCid = [...cid];
  const registerStatus = {status: "",change:[]}
  const changeNeeded = (cash - price).toFixed(2);
  const getChange = (changeNeeded,cid) =>{
      //this function "getChange" was made after watching this video https://www.youtube.com/watch?v=32kJ23O9Wbk 
    const change =  [];
    const currency = {
      "PENNY":0.01,
      "NICKEL":0.05,
      "DIME":0.10,
      "QUARTER":0.25,
      "ONE":1,
      "FIVE":5,
      "TEN":10,
      "TWENTY":20,
      "ONE HUNDRED":100,
    }
    for(let i = cid.length - 1;i >= 0;i--){
      const name = cid[i][0];
      const total = cid[i][1];
      const value = currency[name];
      // console.log(value);
      // console.log(changeNeeded);
      let amount = (total / value).toFixed(2);
      let toReturn = 0;
      // console.log(`here3`)
      while(changeNeeded >= value && amount > 0){
        changeNeeded -= value;
        changeNeeded = changeNeeded.toFixed(2);
        amount--;
        // console.log(`here2`)
        toReturn++;
      }
      if(toReturn > 0){
        // console.log(`here`);
        change.push([name,toReturn * value]);
      }
    }
    return change;
  }
  const changeValue = getChange(changeNeeded,cid);
  
  let sum = 0;
  changeValue.forEach(e =>{
    sum+=e[1];
  })
  // console.log(cid);
  // console.log(changeValue[0][0]);
  const getNewChange = (cid,changeValue) =>{
    let cidClone = cid;
    const objChange = {};
    for(let i = 0;i<changeValue.length;i++){
      objChange[changeValue[i][0]] = changeValue[i][1];
    }
    for(let i = 0;i<cidClone.length;i++){
      cidClone[i][1] = (cidClone[i][1] - objChange[cidClone[i][0]]);
      if(isNaN(cidClone[i][1])){
        cidClone[i][1] = 0;
      };
    }
    return cidClone;
  }
  const newCid = getNewChange(cid,changeValue);
  let x = false;
  if(!newCid.every( e =>{
    e !== 0;
  })){
    registerStatus.status = register.close;
    registerStatus.change = cid;
  }
  if(sum.toFixed(2) < changeNeeded){
    registerStatus.status = register.insufficient;
    registerStatus.change = [];
    return registerStatus;
  }
  
  if(sum.toFixed(2) == changeNeeded){
    registerStatus.status = register.open;
    registerStatus.change = changeValue;
    return registerStatus;
  }
}
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

sorry for being so messy.

I have manage to fix it,as someone pointed out on reddit the problem was the individual inner arrays was pointing to the original memory location,so,the fix would be:

const newCid = cid.map((e) =>{ return
[...e];})
1 Like

Congratulations on finding the solution! Happy coding.

more than one way to clone an array …

var cid=[["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

var map = cid.map((e) =>{ return [...e];});
console.log(map)
map=[];
var filter = cid.filter((e) =>{ return [...e];});
console.log(filter)
filter=[];
var newAr = new Array(...cid);
console.log(newAr)
newAr=[]
var sli = cid.slice()
console.log(sli)
sli=[]
console.log(cid);