Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Update from Synthetic Event

Events & Formsmedium

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

Implement `updateFromEvent(state, event)` where `event` looks like `{ target: { name, value } }`, returning new state with that field updated. This mirrors a React onChange handler.

Sample tests

Input: updateFromEvent({"name":"a"}, {"target":{"name":"name","value":"b"}})
Output: {"name":"b"}
Input: updateFromEvent({"a":"1","b":"2"}, {"target":{"name":"b","value":"9"}})
Output: {"a":"1","b":"9"}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Reading event.value instead of event.target.value.
  • Mutating state.

Learning resources

  • React: Reacting to Input with State
Approach & explanation (try first)

Destructuring { name, value } from event.target and returning { ...state, [name]: value } is exactly how a shared React onChange handler updates the right field.

Loading...
⌘/Ctrl + Enter

Run your code to see results.