Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Toggle Boolean Field

Immutable Statemedium

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 111: The Spread Operator (...)

Implement `toggleField(state, key)` returning a new object with the boolean at `key` flipped.

Sample tests

Input: toggleField({"open":false}, "open")
Output: {"open":true}
Input: toggleField({"open":true}, "open")
Output: {"open":false}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Mutating state[key] = !state[key] keeps the same object reference.

Learning resources

  • React: Updating Objects in State
Approach & explanation (try first)

Negate the current value and return it in a new object, the immutable way to flip a boolean flag in state.

Loading...
⌘/Ctrl + Enter

Run your code to see results.