Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Destructure and Swap

Data Structures, Operators & Stringseasy

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

Implement `swap(pair)` that takes a two-element array `[a, b]` and returns `[b, a]` using destructuring.

Sample tests

Input: swap([1,2])
Output: [2,1]
Input: swap(["hello","world"])
Output: ["world","hello"]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Trying to swap in place without a structure to return.

Learning resources

  • MDN: Destructuring assignment
Approach & explanation (try first)

Returning the reversed pair is the destructuring swap idiom.

Loading...
⌘/Ctrl + Enter

Run your code to see results.