I am trying to write a function that will take two objects, people in this case, and swap only two of their attributes. Here is my function for the Person:
function Person (tall, weight, gender, iq, favoriteColor) {
this.tall = tall;
this.weight = weight;
this.gender = gender;
this.iq = iq;
this.favoriteColor = favoriteColor;
}
var joe = new Person("6-2", 180, "male", 130, "blue");
var bob = new Person("5-11", 150, "male", 120, "red");
So i want to swap JUST their IQs and favoriteColor.
So far I have:
function Swap(a, b) {
var i = a;
a = b;
b = i;
console.log(a, b);
}
This obviously swaps all their properties, but i cannot figure out how to swap just two and still have them log as objects. I have tried:
function Swap(a, b) {
var a = a.iq + a.favoriteColor;
var b = b.iq + b.favoriteColor;
var i = a;
a = b;
b = i;
console.log(a, b);
}
but this returns in the console:
120red 130blue.
Technically it swapped the two values, but their structure as an object is gone and the other three properties that they were supposed to keep of their own are also gone. How do I write the swap function to do this?
Thanks!