Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Partition

Working With Arraysmedium

Course · Section 11: Working With Arrays · Lecture 156: Data Transformations: map, filter, reduce

Implement `partition(nums, threshold)` returning `[below, atOrAbove]`, where `below` holds values less than `threshold` and `atOrAbove` holds the rest, each preserving order.

Sample tests

Input: partition([1,5,2,8], 4)
Output: [[1,2],[5,8]]
Input: partition([1,2], 5)
Output: [[1,2],[]]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Using <= when the boundary value should go into atOrAbove.

Learning resources

  • MDN: Array iteration methods
Visualize this concept: Sorting Arrays →
Approach & explanation (try first)

One pass routes each value into below (less than threshold) or atOrAbove, returned as a tuple.

Loading...
⌘/Ctrl + Enter

Run your code to see results.