DEV Community

Discussion on: initLogs 1: Swapping numbers and reversing arrays in JavaScript

Collapse
 
unsungnovelty profile image
Nikhil • Edited

Hi @steffi,

Thanks for reading my post. I will admit that the const reverseArrayInPlace = (array) => {, the first line was missing the = sign. But it is otherwise working for me. Even with even number of elements. Very curious about your finding. Could you please share the code if interested? :)

const reverseArrayInPlace = (array) => {
  for (let i = 0; i <= Math.floor(array.length / 2); i++) {
      let temp = array[i];
      array[i] = array[array.length - 1 - i];
      array[array.length - 1 - i] = temp;
  }
  console.log(array);
}

reverseArrayInPlace([1,6,4,7, 5, 7, 9, 13 ,86, 12]);

// Output -> [ 12, 86, 13, 9, 5, 7, 7, 4, 6, 1 ]
Enter fullscreen mode Exit fullscreen mode