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

63
Views
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 answers
Answer question

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 Report

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 Report

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 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!