29 Try it Out!


#1

this is my code

...
// 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(alice > billy) {
console.log(alice)
}else{
console.log(billy)
}
}

// 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));
...
my error is
Oops, try again. Your olderAge function seems to be returning something other than a number. The function should return the AGE of the older person


#2

write your olderAge function in general terms, you don't want it to only work for Alice and Billy. Use the format you already used in your ageDifference function.


#3

You want to be using the function as the parameters instead of calling each string.
You can refer to this.

// 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
function olderAge (person1, person2) {
    if (person1.age > person2.age) {
        return person1.age;
    }   
    else 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));

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.