2. Try it Out!: Function Not Recognised


#1

https://www.codecademy.com/en/courses/spencer-sandbox/4/4?curriculum_id=506324b3a7dffd00020bf661#

TypeError: olderAge is not a function

I get this error. Why is the olderAge function not being recognized as one?



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



olderAge(alice, billy);



#2

Can only be called after the function is defined. This is before.

It has to do with hoisting. The name of the function is hoisted in the first pass, but the expression itself is not hoisted, and gets parsed on the second pass.


#4

Thanks for the help. I was looking very closely for syntax errors and did not see that could be the issue since everything looked fine. That definitely makes sense.

I just have to remember to declare all functions and variables before using them in anyway or else they will appear undefined.


#5

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