← All Exercises
Apply a State Reducer Without Mutation
basicjavascript
Background
Reducers are the testable core of React state. Mutating state in place makes renders unreliable; returning new objects makes every transition reproducible.
Problem
Apply a sequence of actions to a state object with a pure reducer, where unknown actions leave state unchanged.
Input
JSON object: { state: object, actions: [{ type, payload? }] }Output
JSON object: the final state
Sample tests
Sample 1
Input: {"state":{"count":0,"label":"idle"},"actions":[{"type":"increment"},{"type":"increment"},{"type":"setLabel","payload":"running"}]}Expected: {"count":2,"label":"running"}Sample 2
Input: {"state":{"count":1},"actions":[{"type":"unknown"}]}Expected: {"count":1}Your solution
Learning objective
Write reducers that are pure and exhaustively testable.