Explorer
Node.js

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 }; } ```

Array Destructuring

Easy
Write a function `getFirstAndLast(arr)` that uses destructuring to return an object with `first` and `last` properties from the array.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[{"first":1,"last":3},{"first":"a","last":"d"}]