This time try making your own function that takes objects as parameters!
Here we have given you the Personconstructor again, along with theageDifference function as an example.
Now create a new function, olderAge. It should take two Person objects as parameters, and return the age of whatever Person is older. For example, with 30 year-old alice and 25 year-old bob, olderAge(alice, bob); should return 30, because that isalice's age and she is older than bob. If the two people have the same age then you can return that age.
Instructions
Define a function called
olderAge
. We want the function to return the age of the person who is older.
// 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;
};
// Make a new function, olderAge, to return the age of
// the older of two people
var olderAge = function(person1, person2) {
if (person1.age > person2.age) {
return person1.age;
} else {
return person2.age;
}
};
// Let's bring back alice and billy to test our new function
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);
console.log("The older person is " + olderAge(alice, billy));
No comments:
Post a Comment