2

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!

Enrique Moreno Tent
  • 24,127
  • 34
  • 104
  • 189
Brandon
  • 335
  • 2
  • 4
  • 15
  • 1
    On this line: `var a = a.iq + a.favoriteColor;` you are declaring a variable (local to the function) with only two values (iq + favoriteColor) and logging it. Note this is not the same variable passed in as a parameter. A better practice is to call it something else (ditto for `var b...`) – blurfus Dec 19 '14 at 22:00

4 Answers4

3

This is what you want. You need to swap the individual fields. You can't add them like you are doing. Also, it's best not to use the same name for variables that you've already declared, it's just too confusing.

function Swap(a, b) {
  var i = a.iq;
  a.iq = b.iq;
  b.iq = i;

  i = a.favoriteColor;
  a.favoriteColor= b.favoriteColor;
  b.favoriteColor = i;
  console.log(a, b);
}
imtheman
  • 4,713
  • 1
  • 30
  • 30
1
function Swap(a, b) {
  var newBIq = a.iq
  var newBColor = a.favoriteColor;
  var newAIq = b.iq
  var newAColor = b.favoriteColor;
  a.iq = newAIq;
  a.favoriteColor = newAColor;
  b.iq = newBIq;
  b.favoriteColor = newBColor
  console.log(a, b);
}

Here you see the 4 values needed. I hope the naming helps understanding better than just i,j :D

ProgrammingIsAwsome
  • 1,109
  • 7
  • 15
1

If we take this solution to swap values...

b = [a, a = b][0];

...we could do this...

function Swap(a, b) {
    b.iq = [a.iq, a.iq = b.iq][0];
    b.favoriteColor = [a.favoriteColor, a.favoriteColor = b.favoriteColor][0];
}

and if you can use ECMAScript 6, you could even do

function Swap(a, b) {
    [a.iq, b.iq] = [b.iq, a.iq]
    [a.favoriteColor, b.favoriteColor] = [b.favoriteColor, a.favoriteColor]
}
Community
  • 1
  • 1
Enrique Moreno Tent
  • 24,127
  • 34
  • 104
  • 189
0

This will swap all the values for the keys in an array of keys between objects a and b:

function Swap(a,b, keys){
    for(var i=0; i<keys.length; i++){
        var currentKey = keys[i];
        var temp = a[currentKey];
        a[currentKey] = b[currentKey];
        b[currentKey] = temp;
    }
}
ateich
  • 520
  • 3
  • 10
  • 2
    In this case the keys can be passed in as an array `Swap(joe, bob, ['iq', 'favoriteColor'])`. This makes the function much more flexible and useful. – musicfuel Dec 19 '14 at 22:04
  • 1
    This does not swap all values. It only swaps the values of specified keys. E.g. Swap(bob, joe, ['iq', 'favoriteColor']); will swap bob's and joe's IQ and favorite color only. – ateich Dec 19 '14 at 22:04