Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Apply Action Sequence

Hooks Logicmedium

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

Implement `applyActions(initial, actions)` folding a list of `{ type }` actions (`inc` or `dec`) over a counter starting at `initial`.

Sample tests

Input: applyActions(0, [{"type":"inc"},{"type":"inc"},{"type":"dec"}])
Output: 1
Input: applyActions(5, [])
Output: 5

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Mutating an external counter instead of folding immutably.

Learning resources

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

Folding the action list with reduce applies each inc or dec in order, mirroring how a useReducer would process a sequence of dispatches.

Loading...
⌘/Ctrl + Enter

Run your code to see results.