How to implement Array.prototype.reverse? JavaScript Interview Question | Problem Solving | JavaScript Polyfills
In this question, the candidate needs to implement a function customReverse
that mimics the behaviour of Array.prototype.reverse
method.
More about Array.prototype.reverse
The reverse()
method reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.
Examples
const array = ['one', 'two', 'three'];
console.log('array:', array);
// expected output: "array:" Array ["one", "two", "three"]
const reversed = array.reverse();
console.log('reversed:', reversed);
// expected output: "reversed:" Array ["three", "two", "one"]
// Careful: reverse is destructive -- it changes the original array.
console.log('array:', array);
// expected output: "array:" Array ["three", "two", "one"]
Syntax
reverse()
Return value
The reference to the original array, now reversed. Note that the array is reversed in place, and no copy is made.
Submission
Start the timer, complete your solution, and test it against the test cases provided by the platform. Ideally, you should finish this question within 30 mins. Share your solution with us -- https://twitter.com/devtoolstech