Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Form Reducer

Reducersmedium

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

Implement `formReducer(state, action)` where a `setField` action carries `field` and `value`, updating that field of the form object immutably.

Sample tests

Input: formReducer({"name":""}, {"type":"setField","field":"name","value":"Al"})
Output: {"name":"Al"}
Input: formReducer({"name":"x","age":"1"}, {"type":"setField","field":"age","value":"2"})
Output: {"name":"x","age":"2"}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Mutating the form object directly.

Learning resources

  • React: useReducer
Approach & explanation (try first)

A form reducer updates one field per action by returning a new object with that computed key replaced, the pattern behind controlled multi-field forms.

Loading...
⌘/Ctrl + Enter

Run your code to see results.