Need help PASSING OBJECTS into functions!


#1

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 = (billy,alice){
 return ageDifference person1.age - person2.age
};

#2

You can call a function like so,

function_name(a_argument_you_want_to_pass_to_it, and_another)

In this case, the arguments you want to pass are billy and alice.


#3

When the ageDifference function was created it was setup to expect two pieces of data to be sent to it. That's this part here: (person1, person2)

To use the function, you call it, and send it the pieces of data that it is expecting, like this:

ageDifference(alice,billy);

If you also want to see the results you can log the function call, like this:

console.log(ageDifference(alice,billy));

Do you see how these pieces fit together now?


#4

I got it running thanks


#5

how did u get it?
can u expain please ?
and share ur code also


#6

Have look at my earlier post that explains how to use your ageDifference function. Instead of creating a new function, use that one instead.