Write a function that takes an array as input and returns a new array with the last element removed. The original array should not be modified. Your solution should return a new array that contains all the elements of the input array except for the last one.
Input: array = [1, 2, 3, 4, 5]
Output: [1, 2, 3, 4]
function removeLastElementWithSlice(arr) {
return arr.slice(0, -1);
}function removeLastElementWithFilter(arr) {
return arr.filter((_, index) => index < arr.length - 1);
}function removeLastElementWithArrayFrom(arr) {
return Array.from(arr).slice(0, -1);
}function removeLastElementWithMap(arr) {
return arr.map((item, index) => (index < arr.length - 1 ? item : null)).filter(item => item !== null);
}function removeLastElementWithReduce(arr) {
return arr.reduce((acc, item, index) => {
if (index < arr.length - 1) acc.push(item);
return acc;
}, []);
}function removeLastElementWithForLoop(arr) {
let newArray = [];
for (let i = 0; i < arr.length - 1; i++) {
newArray.push(arr[i]);
}
return newArray;
}function removeLastElementWithForOf(arr) {
let newArray = [];
let index = 0;
for (const item of arr) {
if (index < arr.length - 1) {
newArray.push(item);
}
index++;
}
return newArray;
}function removeLastElementWithSplice(arr) {
return arr.slice(0, -1);
}function removeLastElementWithConcat(arr) {
return [].concat(arr.slice(0, -1));
}function removeLastElementWithDestructuring(arr) {
const [ ...newArray ] = arr;
newArray.pop(); // Removes the last element
return newArray;
}