Switching key value pairs

I’m trying to write a function that takes an object as an argument and returns a new object with the key/value pairs from the argument object switched around

For example: switchPair({food: ‘burger’}); returns {burger: ‘food’}

Here’s my code:

function switchPair(args) {
  let emptyObj = {};
  for(let key in args) {
    return emptyObj[args[key]] = key;
  return emptyObj;

switchPair({food: 'burger'});

why do you use return in the for loop?

the loop needs to swap the keys and values, then after the loop you can return the new object.

1 Like