Writing an Array Extension
Write a function that creates a palindrome of any array.
We'll cover the following...
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
Press + to interact
Array.prototype.toPalindrome = function(){//write your code here}
toPalindrome()
returns a new array. It keeps the element arr[arr.length - 1]
the same, and ...