Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Add-by-Amount Reducer

Reducerseasy

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

Implement `amountReducer(state, action)` where an `add` action carries a numeric `payload` to add to the state number. Unknown actions return state unchanged.

Sample tests

Input: amountReducer(0, {"type":"add","payload":5})
Output: 5
Input: amountReducer(3, {"type":"add","payload":-1})
Output: 2

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Assuming a payload exists on every action type.

Learning resources

  • React: useReducer
Approach & explanation (try first)

The reducer adds the payload for an add action and returns the current state for all other action types, keeping it pure and total.

Loading...
⌘/Ctrl + Enter

Run your code to see results.