Is there a typo or what?


#1


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


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


Thanks to all who help


#2

// Make a new function, olderAge, to return the age of
// the older of two people

You've made a function but did not passed the parameters?

Back to theory of this exercise.

Now create a new function, olderAge. It should take two Person objects as parameters, and return the age of whatever Person is older.

also you need to make modifications here..
if(alice > billy)

olderAge() function have any two person Objects,not just alice and billy.


#3

so do I put the Person objects in the if statement?


#4

Two persons right? (As parameters of function)
also I don't get ,How you're using these lines of code.

console.log(person1.age);
console.log(person2.age);

inside the if/else clause.
person1 and person2? where they came from ?


#5

The lesson wants me to return the older age but when I used the return line my error said the return was illegal


#6

This lesson tells you to define a function called olderAge()

That you made right...

var olderAge = function() {
   
};

It also tells you to pass it two Object parameters.
say.. Per1,Per2

It can be done like this..

var olderAge = function(Per1, Per2) {
   
};

Now you have to compare the ages of these two objects and return age of person who is old.

to do this,as you know that Per1 and Per2 are objects.
You can use dot notation to access the ages.

Per1.age
Per2.age

You will use if/else clause to accomplish this.


#7

now the only problem is that the code fails to return the older age


#8

here's my function and if statement

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

and here is what is going into the console, which I probably shouldn't be doing

The older person is 25

all I need the return to do is to return 30 not 25


#9

good work so far! :thumbsup:

We need to make two small changes here..

Point1.

if(person1 > person2)

As we know that person1 and person2 are Objects ,But we had to compare ages,right?
so how we compare it
Simple..
By comparing person1.age and person2.age (As they are ages) ?

Point2.

This code works so far so good but there is a glitch.
We're not testing if ages are same or not?

so introduce one more line and comparing for equality of their ages.

So you will have to introduce an else if too..

so your code will look this.. (Inside the function)

if(ages are same) {
    return anyAge;
} else if(condition) {
    return age; 
} 
else {      
    return age;
}

I hope it helps!


#10

what would the anyAge be? or would it be like the person1.age?


#11

As both persons have same age so you can return any,right?


#12

they don't the lesson has alice as 30 and billy as 25


#13

Your code will work okay for the exercise. (without equality checks As both Person object have different ages)
By introducing an equality check-
It makes your code to handle situations when Objects(Person1,Person2) have same age values.
You can go neglecting my second point.


#14

my code is proper now but the error message still says the same thing


#15

Can you put your final code that throwing error?


#16

this is the only part that is causing the problem

    if (person1 > person2) {
        return person1.age;
    } else if (person1 < person2) {
        return person2.age;
    } else {
        return 30;
    }

p.s. You did say any age


#17

It was an template how your code will look!

if (person1 > person2)
also this line..
else if (person1 < person2)
You still did not made any change here.

what I said i point1?

Point1.

if(person1 > person2)

As we know that person1 and person2 are Objects ,But we had to compare ages,right?
so how we compare it
Simple..
By comparing person1.age and person2.age (As they are ages) ?

Also this line..

 else {
        return 30;
    }

We have to return person2 age here?


#18

here is the return of person2


#19

can you tell me what person1 and person2 represents here ?


#20

Billy and Alice's ages