Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Pagination Slice

Hooks Logicmedium

Course · Section 17: Modern JavaScript Development: Modules, Tooling, and Functional · Lecture 294: Declarative and Functional JavaScript Principles

A pagination hook returns just the rows for the current page. Implement `paginate(items, page, perPage)` returning the slice of `items` for the 1-based `page`. Pages past the end return an empty array.

Sample tests

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

+ 2 hidden tests run on Submit.

Hints

Common pitfalls
  • Using page directly (without subtracting 1) skips the first page of results.
  • Manually looping and risking index errors when slice handles the bounds for you.

Learning resources

  • React: Built-in Hooks
Approach & explanation (try first)

Each page is a contiguous window of size perPage. The start index is (page - 1) * perPage, and slice returns that window — automatically returning an empty array when the start is past the end.

Loading...
⌘/Ctrl + Enter

Run your code to see results.