Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Pagination State

Hooks Logicmedium

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

Implement `pagination(state, action)` where `state` is `{ page, total }` and `action` is the string `next` or `prev`. Clamp `page` to the range `1..total`.

Sample tests

Input: pagination({"page":1,"total":3}, "next")
Output: {"page":2,"total":3}
Input: pagination({"page":1,"total":3}, "prev")
Output: {"page":1,"total":3}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Letting page exceed total or drop below 1.
  • Mutating the state object.

Learning resources

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

Clamping with Math.min(page + 1, total) and Math.max(page - 1, 1) keeps pagination within bounds while returning new state objects.

Loading...
⌘/Ctrl + Enter

Run your code to see results.