Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Split First and Rest

Data Structures, Operators & Stringsmedium

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 112: Rest Pattern and Parameters

Implement `splitFirst(arr)` returning `[first, rest]` where `rest` is an array of the remaining elements, using a rest element.

Sample tests

Input: splitFirst([1,2,3])
Output: [1,[2,3]]
Input: splitFirst([5])
Output: [5,[]]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • The rest element must be last in the pattern.
Visualize this concept: Spread and Rest →
Approach & explanation (try first)

The rest pattern gathers everything after the first element into a new array.

Loading...
⌘/Ctrl + Enter

Run your code to see results.