2

I am trying to create a copy of original array that i have received as an argument passed to the function. I am performing operations on the copied array but still my main array is getting mutated.I don't want to change the value of cid that i received originally. I have tried a plenty of ways like copying through slice(),concat() and spread operator but still my main (cid)array is getting mutated. Help me to get rid of this. Thanks in advance..

  let copierFunction=(...a)=>{
    let b= [...a]
    return(b)
  }
  let originalCID = cid.filter((e)=>{
    if(e){
      return e
    }
  })
  let cashInDrawer=copierFunction(...cid);
  let toBeReturned = cash-price;

  let currencyRefrence= [['PENNY',0.01],['NICKEL',0.05],['DIME',0.1],['QUARTER',0.25],['ONE',1],['FIVE',5],['TEN',10],['TWENTY',20],['ONE HUNDRED',100]];

  currencyRefrence = currencyRefrence.reverse()

  let change=[]
  function returnChange(a){
    if(a==0){
      return null;
    }
    for(let i=0;i<currencyRefrence.length;i++){
      let returnUnit = a / currencyRefrence[i][1]
      if(returnUnit>=1){
        let chutte =  parseInt(returnUnit)*currencyRefrence[i][1];

        for(let j=0;j<cashInDrawer.length;j++){
          if(cashInDrawer[j][0] == currencyRefrence[i][0]){
            if(cashInDrawer[j][1]>=currencyRefrence[i][1]){
              if(cashInDrawer[j][1]>chutte){
              change.push([cashInDrawer[j][0],chutte])
              a= a-chutte;
              a= a.toFixed(2)
              cashInDrawer[j][1]= cashInDrawer[j][1]-chutte
              return returnChange(a);
              }else{
                change.push([cashInDrawer[j][0],cashInDrawer[j][1]])
              a= a-cashInDrawer[j][1];
              a= a.toFixed(2)
              cashInDrawer[j][1]= 0;
              return returnChange(a);
              }
            }
          }
        }

      }
    }
  }
  returnChange(toBeReturned);

  function statusChecker(cashInDrawer,change){

    //checking if change is given or not.
    let returnedAmount=(change.reduce((a,e)=>{
      return( a + e[1])
    },0)).toFixed(2)
    if(toBeReturned > returnedAmount ){
      return 'INSUFFICIENT_FUNDS'
    }

    //checking if cashInDrawer is empty after change.
    let amountIncashInDrawer = (cashInDrawer.reduce((a,e)=>{
      return( a + e[1])
    },0)).toFixed(2) 
    if(amountIncashInDrawer==0){
      return 'CLOSED'
    }
    return 'OPEN';

  }

 let status = statusChecker(cashInDrawer,change)
  if(status =='INSUFFICIENT_FUNDS'){
    return {status:status,change:[]}
  }
  if(status == 'CLOSED'){
    
    return {status:status,change: 'originalCID'}
  }
  return {status:'OPEN',change:change}


}

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]])); ```
4
  • 5
    Spread is not a deep clone/copy. Commented Mar 28, 2021 at 19:54
  • please add working code. Commented Mar 28, 2021 at 19:55
  • You spread the same argument 3 times. I don't know if there is any reason for that, but it looks incorrect. Please share the original data. Commented Mar 28, 2021 at 19:55
  • I would really like to thank you all for helping me in this... I am new to JS. I really appreciate that you all took time out of your important schedule to help me out in this. Thing is I am trying to solve this question of freecodecamp.. freecodecamp.org/learn/… Commented Mar 29, 2021 at 20:17

2 Answers 2

2

JS arrays are passed-by-reference. You need to use:

let arrayCopy = JSON.parse(JSON.stringify(array));

This way you can do operations on arrayCopy, while your original array wont change

Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much for helping me out of this problem :)
So have you solved it? I looked at it and i dont think you need to copy any arrays in this assignment. Also its good to dissmantle the problem first and test out the parts and post only the parts which u cant figure out to stackoverflow.
Yes I solved it.. Using a different approach. Actually I am new to programming and also Stackoverflow. Bro thanks for advice. I will follow it while posting questions on stackoverflow. I would really like to appreciate your time..I am very impressed that people here at stackoverflow are helping each other even though they don't know each other or never met with each other. :)
0

JS arrays are simple objects in memory, and when passed as an argument in first class functions, it's reference will gets passed. So any changes to that referenced array will also change the values of actual array. instead you can create a new array and spread syntax.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.