Writing an Array Extension

Write a function that creates a palindrome of any array.

Suppose an array of numbers is given. Create a method toPalindrome that creates a palindrome out of your array in the following way:

const arr = [1,2,3];
//[1, 2, 3]
const arr2 = arr.toPalindrome()
//[1, 2, 3, 2, 1]
const arr3 = arr2.toPalindrome()
//[1, 2, 3, 2, 1, 2, 3, 2, 1]
console.log( arr, arr2, arr3 );
//[1, 2, 3] [1, 2, 3, 2, 1] [1, 2, 3, 2, 1, 2, 3, 2, 1]
//undefined

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.