Weird Error in 29/33 Try it Out


#1

Uploading...

Error message: It looks like your olderAge function fails when the first Person is older! It should return the age of the older person

Here 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(){
if (alice.age > billy.age){
return alice.age
} else if(alice.age< billy.age){
return billy.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));
`


#2

I solved it

This is the correct function you must type.

var olderAge = function(person1,person2){
if (person1.age > person2.age){
return person1.age
} else {
return person2.age
}
};


#3

This is a dedicated function that will only work with the two object named. Ideally a function will take any two objects that have an age attribute.

var olderAge = function (person1, person2) {
    if (person1.age > person2.age) {
        return person1.age;
    } else {
        return person2.age;
    }
};

Now we can call it with any two objects:

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

#4

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