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

95
Views
Assign array element to a variable, then modify array element by modify variable but don't work

So I want to do expression on variable instead of directly array element because I think

  1. The compiler will re-calculate to find the array element
  2. Naming variable is easy to understand than arr[idx]

But when I attempt to swap 2 element in the array using variable, it don't work (see the code below).

I can't think of the keyword to this situation. Google results are all deep/shallow copy array.

Any explanation?

for (let i = 0, j = 1; i < numsClone.length && j < numsClone.length;) {
    let numHasEvenIdx = numsClone[i];
    let numHasOddIdx = numsClone[j];

    if (numHasEvenIdx % 2 === 1 && numHasOddIdx % 2 === 0) {
      [numsClone[i], numsClone[j]] = [numsClone[j], numsClone[i]]; 
      // I want this to be [numHasEvenIdx, numHasOddIdx] = [numHasOddIdx, numHasEvenIdx]
      i += 2;
      j += 2;
    }
    else if (numHasEvenIdx % 2 === 0) i += 2;
    else j += 2; //(numHasOddIdx % 2 === 1)

}

about 4 years ago · Juan Pablo Isaza
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!