Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Todo Toggle Reducer

Reducersmedium

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

Implement `todoToggleReducer(state, action)` where a `toggle` action carries an `index`, flipping that todo's `done` flag immutably.

Sample tests

Input: todoToggleReducer([{"text":"a","done":false}], {"type":"toggle","index":0})
Output: [{"text":"a","done":true}]
Input: todoToggleReducer([{"text":"a","done":true},{"text":"b","done":false}], {"type":"toggle","index":1})
Output: [{"text":"a","done":true},{"text":"b","done":true}]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Mutating the matched todo object.
  • Toggling by reference equality instead of by index.

Learning resources

  • React: useReducer
Approach & explanation (try first)

map returns a new array; at the target index you return a new todo with done flipped, and every other todo is returned as-is, so only one item changes identity.

Loading...
⌘/Ctrl + Enter

Run your code to see results.