Swap Keys and Values
Write the code, see it run, pass the checks.
The problem
- easy
- Code
Write invert(obj), where obj is a plain object whose values are strings or numbers. Return a new object in which each value becomes a key (a number becomes its string form, as object keys always do) and maps back to the key it came from. When several keys share a value, the one that comes last in obj's key order wins. Don't change obj.
Your code must pass
- Swaps keys and values
- The last key wins when values repeat
- Returns a new object and leaves the original alone
- Returns an empty object for an empty object