Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
Function changes the value of parameter unexpectedly

I wrote a function calcNewtonPath with 4 parameters as below. However when i call it, the function changes the value of parameter matA unexpectedly and i can't understand why.

function calcNewtonPath(matA,matB,m,n){
  let temp = matA.slice()  //i tried this to prevent the unexpected change but it didn't work
  for(let i=0;i<m;i++){
    temp[i].push(-matB[i])
  }
  return matrix.solve(temp,m,n)
}

The function solve also changes the value of temp too! But it's a bit complicated so i think i will not put the code here. Can anyone help me about this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

UPDATED:

.slice() will just do a shallow copy.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

That means for example:

const a = [[1,2,3]]
const b = slice.a();

b[0][1] = 4;

a array will also be changed.

a will be [[1,4,3]] as well as b;

One easy solution would be to make a deep copy of an array:

const deepCopy = JSON.parse(JSON.stringify(array));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!