Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Add to Both Ends

Fundamentals: Part 2easy

Course · Section 3: JavaScript Fundamentals – Part 2 · Lecture 41: Basic Array Operations (Methods)

Implement `addEnds(arr, first, last)` returning a new array with `first` prepended and `last` appended.

Sample tests

Input: addEnds([2,3], 1, 4)
Output: [1,2,3,4]
Input: addEnds([], 1, 2)
Output: [1,2]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • unshift/push mutate the original array.
Approach & explanation (try first)

Spreading the array between the two new values builds a new array without mutation.

Loading...
⌘/Ctrl + Enter

Run your code to see results.