Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Fold Actions over a Counter

Reducerseasy

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

Implement `counterReducer(state, action)` (`inc`, `dec`, `reset`, default unchanged) so it can be folded over an action list with reduce.

Examples

Input: [inc, inc, dec, reset, inc] reduced from 0
Output: 1
0,1,2,1,0,1 — the running result after each action.

Sample tests

Input: reduce a sequence from 0
Output: 1
Input: from a starting value
Output: 7

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Forgetting the default case yields undefined for unknown actions.

Learning resources

  • React: useReducer
Approach & explanation (try first)

Because a reducer is pure, folding it over an action list replays a sequence of dispatches to the final state.

Loading...
⌘/Ctrl + Enter

Run your code to see results.