Operation to test basic understanding of array methods
javascript
Instructions:
Challenge 1: Mutate the below array arr to achieve the expected result
const arr = [1, 2, 3, 4, 5];
// result - [6 , 5, 4, 3, 2, 1, 0]
Challenge 2: Combine two arrays arr1 and arr2 into arr3 and get rid of the extra 5
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [5, 6, 7, 8, 9, 10];
// result - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Solution:
Challenge 1:
const arr = [1, 2, 3, 4, 5];
arr.push(6); // [1, 2, 3, 4, 5, 6] - adds 6 to end
arr.unshift(0); // [0, 1, 2, 3, 4, 5, 6] - adds 0 to start
console.log(arr.reverse()); // reverse and logs the array
Challenge 2:
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [5, 6, 7, 8, 9, 10];
console.log(arr1.concat(arr2.slice(1))); // using slice and concat method
console.log([...arr1, ...arr2.slice(1)]); // using slice and spread operator