13. Intro to Objects, #29, Try IT out!


#1


Intro to Objects, #29, Try It Out!


I keep getting a Reference Error 'Person1 not defined'. Please help!


Replace this line with your code. 
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 Person1("Alice", 30);
var billy = new Person2("Billy", 25);

console.log("The older person is " + olderAge(alice, billy));


#2

Hey there.

var alice = new Person1("Alice", 30);
var billy = new Person2("Billy", 25);

Here it is.

When you make a new Person you should call it just that - Person. (not person1 or person2)

This is because your constructor function is named Person as well. Meaning we're calling that function and passing some parameters to it.

This is why you're getting 'Person1 not defined'. Because you didn't define person1. You defined the Person function and you are assigning that to var alice and var billy.

Hope that clears it up.


#3

Thank you, that did solve it. Appreciate the help.


#4

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