Why is my olderAge function not a function?


#1
// 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


// 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));

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

What is wrong with my code?? I'm getting the error: "TypeError: olderAge is not a function". But it looks like olderAge has everything it needs to be a function! :frowning:


#2

We cannot call a function expression before it is defined. Move this line to below the function and it will work.


#3

Thank you so much mtf!