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

61
Vistas
JavaScript reversing array

function reverseArrayInPlace(array) {
  for (let i = 0; i < Math.floor(array.length / 2); i++) {
    let old = array[i];
    array[i] = array[array.length - 1 - i];
    array[array.length - 1 - i] = old;
  }
  return array;
}


let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]

I am working on the Data Structures of the book Eloquent JavaScript. I mostly understand how this loop works which is cutting the array in half and then start swapping elements at the opposite sides. but here my problem: the first round of the loop my understanding of "array[i]" is at index 0 and "Math.floor(array.length / 2)" is at index 1. So, we set old = index 0 and then override it to "array[array.length - 1 - i]". the question is first what exactly does -i part mean and what index does this "array[array.length - 1 - i]" located at at the first round of the loop. please someone explain it to I am very visual and I can't pass it if I don’t see each statement in action.

almost 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

easier to understand, and without the problem with odd arr.length

function reverseArrayInPlace(arr)
  {
  for (let i= 0, j= arr.length -1; i < j; i++, j--) 
    {
    // swapp values of arr[i] and arr[j]
    [ arr[i], arr[j] ] = [ arr[j], arr[i] ];  
    }
  return arr;
  }


let arrayValues = [1, 2, 3, 4, 5];

reverseArrayInPlace( arrayValues );

document.write( JSON.stringify( arrayValues ));

almost 4 years ago · Santiago Trujillo Denunciar

0

array.length - 1 - i is at the first round array.length - 1 - 0 which is the last element in the array. So the loop swaps the first and the last element, then increments i. Next round the 2nd and the value before the last one is going to be swapped and so on.

Array:             1234567890
1st Iteration i=0: ^        ^
2nd Iteration i=1:  ^      ^
3rd Iteration i=2:   ^    ^
4th Iteration i=3:    ^  ^
5th Iteration i=4:     ^^
almost 4 years ago · Santiago Trujillo Denunciar

0

An array is zero-indexed. Meaning, the first item in an array is at the [0] position. When you use array.length, it returns a total of array elements which can be confusing to you because an array of length 5 will have it's last element located at array[4] (because zero is the first).

For each iteration in your function, it uses the counter i to find the position at the end of the array that matches the iteration.

array.length - 1 - i

The -1 part is explained above. When using array.length, you have to subtract 1 to get the actual position.

almost 4 years ago · Santiago Trujillo Denunciar
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