28. Passing Objects into Functions


#1

Not sure how to proceed here, I am completely lost it seems.
I thought this would be the proper way:
// 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(15){
return alice.age - billy.age;
}
ageDifference();

However I am getting:
SyntaxError: Unexpected number '15'. Expected a parameter pattern or a ')' in parameter list.
Any explanation would be greatly appreciated


#2

var diff = function(15){
return alice.age - billy.age;
}
ageDifference();

The code above makes diff a new function. Since we already have the function ageDifference, delete this chunk of code.

We want to: Complete line 17 by calling ageDifference and saving the result in our global diff variable.

var diff = ageDifference(alice, billy);

I hope this makes sense! :smile:


#3

Makes perfect sense!! Thanks, it gets hard to remember all these things sometimes


#4

Good I'm glad! Remember that everything just takes practice : )