Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Destructure with Defaults

Data Structures, Operators & Stringsmedium

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

Implement `parseCoords(arr)` returning `{ x, y }` from the first two elements, defaulting missing values to `0`.

Sample tests

Input: parseCoords([1,2])
Output: {"x":1,"y":2}
Input: parseCoords([5])
Output: {"x":5,"y":0}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Defaults apply only to missing (undefined) positions.

Learning resources

  • MDN: Destructuring assignment
Visualize this concept: Destructuring →
Approach & explanation (try first)

Destructuring with defaults fills x and y, supplying 0 where the array is short.

Loading...
⌘/Ctrl + Enter

Run your code to see results.