Writing an Array Extension
Explore how to extend JavaScript arrays by adding a toPalindrome method that constructs palindromes from existing arrays. Understand how to manipulate array prototypes, use shallow copies, and apply ES6 spread operators to simplify code.
We'll cover the following...
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
toPalindrome() returns a new array. It keeps the element arr[arr.length - 1] the same, and ...