The reverse() method reverses an array in place and returns the reference to the same array.
β Implementation
Array.prototype.customReverse = function() { let left = 0; let right = this.length - 1; // Swap elements from both ends towards the center while (left < right) { [this[left], this[right]] = [this[right], this[left]]; // Swap left++; right--; } return this; // Return the modified array (reverse is in-place) }; // Example usage: const array = ['one', 'two', 'three']; console.log('array:', array); // Output: ["one", "two", "three"] const reversed = array.customReverse(); console.log('reversed:', reversed); // Output: ["three", "two", "one"] // The original array is also reversed since the method modifies it in place // console.log('array:', array); // Output: ["three", "two", "one"]
Key Features:
- In-place Reversal : The array is reversed directly, modifying the original array.
- Two-pointer Approach : Efficient swapping using two pointers (
leftandright) that move towards each other. - Time Complexity :
O(n)βeach element is swapped once. - Space Complexity :
O(1)βno additional memory is used, other than the variables for the pointers.
This method efficiently reverses the array while mimicking the behavior of the native .reverse() method in JavaScript.
Quick Quiz
Test your understanding with 3 quick questions
Q1Does `reverse()` modify the original array or return a new one?
Q2What is the time complexity of the two-pointer reverse algorithm?
Q3What is the space complexity of the in-place reverse polyfill?