Why doesn't this work? 29/33


#1

So, my code is correct and I passed this exercise, however just to experiment I wanted to make the console print "The older person is (name) who is (age), instead of it just saying "The older person is (age). Just to make it a little better.

In my code below I added the string to my return statements to mention the persons name, in the olderAge function, but it doesn't print correctly. Any idea why?

/ 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 (person1, person2){
    if(person1.age > person2.age2){
        return "alice who is " + person1.age;
    }
    else if(person1.age < person2.age){
        return "billy who is " + person2.age;
    }
    else{
        return "alice and billy are the same 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

Hi it's because the Lesson want you to..
Define a function called olderAge. We want the function to return the age of the person who is older.
SO if you add strings to it it will see it as an error.


#3

Ah I see, so my code isn't wrong, it's just not what the lesson specifically wanted. Thanks


#4

Yes it isn't wrong..


#5

Technically it is wrong, you've hardcoded who person1 and person2 will be. What if someone asked "olderAge(billy, alice)" instead?

Also, you had

if(person1.age > person2.age2){

While there's no such "age2"

For YOUR example, the correct thing to return as a string would be

   if(person1.age > person2.age){
        return person1.name + " who is " + person1.age;
    }
    else if(person1.age < person2.age){
        return person2.name + " who is " + person2.age;
    }
    else{
        return person1.name + " and " + person2.name + " are the same age";
    }

So yeah, your logic worked, but your return was hardcoded to provide a this.name which can cause confusing answers.


#6

there are two errors :
return "alice and billy are the same age"; should be replaced by return person1.age
and
if(person1.age > person2.age2){ should have person2.age only