Passing Objects into Functions


#1

// Our person constructor
function Person (name, age) {
this.name = name;
this.age = age;
}

// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
return person1.age - person2.age;
}

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

// get the difference in age between alice and billy using our function
var diff = function (alice, billy) {
return alice.age - billy.age;
}


#2

diff should hold the difference in age between alice and billy. We already have a function that does it, you just need to call it specifically for alice and billy.


#3

var diff = ageDifference(alice.age, billy.age);
I called to function like this, and it ended with "Oops, try again. Make sure that diff is equal to the result of calling ageDifference with alice and billy as the arguments!"


#4

You're already returning the difference between the age properties. You should just pass the persons to the function, not their ages.


#5

var diff = ageDifference(alice,billy);


#6

// Nosso construtor person
function Person (name, age) {
this.name = name;
this.age = age;
}

// Podemos fazer uma função que toma pessoas como argumentos
// Esta computa a diferença de idade entre duas pessoas
var ageDifference = function(person1, person2) {
return person1.age - person2.age;
}

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

// obtenha a diferença de idade entre alice e billy usando nossa função
var diff = ageDifference(alice,billy);
console.log (diff)