Write a function `getFirstAndLast(arr)` that uses destructuring to return an object with `first` and `last` properties from the array.
Problem Statement
Write a function `getFirstAndLast(arr)` that uses destructuring to return an object with `first` and `last` properties from the array.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Array destructuring lets you unpack values from arrays: const [a, b] = [1, 2].
š” Hint 2:
To get the last element, you can use arr[arr.length - 1] or arr.at(-1).
š” Hint 3:
Combine destructuring for the first element and index access for the last.
ā
Solution:
```javascript
function getFirstAndLast(arr) {
const [first] = arr;
const last = arr[arr.length - 1];
return { first, last };
}
```