Question About (15- Exercise 1- Address Object )

So, with all the exercises been giving it a solid effort to do it myself and this is what I was able to come up with on my own for (15- Exercise 1- Address Object) javascript course part 1.

const address = {
  street: "a",
  city: "b",
  zipCode: "c",
};

function showAddress(address) {
  const message = `${address.street}\n${address.city}\n${address.zipCode}`;
  return message;
}

console.log(showAddress(address));

So, for his solution he used a forin loop. Which leads me to think my way is incorrect? so my question is whats the different between doing it my way and his way? included his solution below.

const address = {
  street: "a",
  city: "b",
  zipCode: "c",
};

function showAddress2(address2){
    for (let key in address2) {
        console.log(key, address2[key]);
    }
}

showAddress2(address);

Your solution is not “incorrect”. It’s just different. And you did exactly the right thing in also trying out Mosh’s solution. Compare both approaches, see their pros and cons and learn from it.

1 Like