Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Props with Defaults

Props & Classeseasy

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

Implement `withDefaults(props, defaults)` returning props merged over defaults, so provided props win.

Sample tests

Input: withDefaults({"a":1}, {"a":0,"b":2})
Output: {"a":1,"b":2}
Input: withDefaults({}, {"x":5})
Output: {"x":5}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Spreading props before defaults, which lets defaults clobber real values.

Learning resources

  • React: Passing Props to a Component
Approach & explanation (try first)

{ ...defaults, ...props } merges so that any prop the caller passed wins, while unspecified ones fall back to the default.

Loading...
⌘/Ctrl + Enter

Run your code to see results.