swapValues

Easy · Object Manipulation · 2 test cases

Given an object with two properties 'a' and 'b', swap the values of 'a' and 'b'. Return the modified object.

Examples

swapValues({a: 1, b: 2}) → {a: 2, b: 1}
swapValues({a: 'x', b: 'y'}) → {a: 'y', b: 'x'}

Starter code

function swapValues(obj) {

}

Complexity of the optimal solution

Time O(1), space O(1). The solution uses array destructuring to swap the values of two properties. This is a constant time operation and modifies the object in-place, so space complexity is also constant.

Solve swapValues in the browser