28. Passing Objects into Functions


#1



Oops, try again. Make sure to call the ageDifference function.
// 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 = new ageDifference(alice,billy);


Help me please!


#2

Got a question? Explain what has got you stuck and what it is you want to know so that you can figure it out.

A good question is easy to reply to, it's specific and shows having tried yourself, shows what your train of thought is so that it's possible to tell where you're going wrong or what you're missing. A good question gets a better reply and faster.


#3

Hey coolcoolkg21,

In your code

var diff = new ageDifference(alice,billy);

you do not need to make it a new Object since the function was already created earlier in the code. If the function is already created what do you have to do to call it. Your code is really close!


#4

Exactly, it does not need the 'new' because you are calling a function that already exists.
It should look like this:

var diff = ageDifference(alice, billy)


#5

please I have a question why the (a) in alice is lowercase, while we introduce it as a capital case "Alice"


#6

sorry guys, I get it I miss understand it


#7

// 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 = ageDifference(alice, billy)

console.log("Age difference: " + diff)


#8

hi, my code run with this:

var diff = ageDifference(alice, billy);
console.log("Age difference: " + diff)


#9

i wrote the code as this :slight_smile:
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 = ageDifference(alice,billy);{
console.log()}

ok .pass the lesson