Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda