0

I am working on a codecademy javascript course, and in this one i have to return the older age of two people. For some reason it is returning the younger age, cant figure it out for the life of me.

// 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
function olderAge(){
    if(alice > billy){
        return alice.age;
    }
    else{
        return billy.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));
1
  • How exactly you can define if an object is greater than another? Commented Jun 14, 2015 at 16:24

2 Answers 2

2

Your function that compares ages is comparing the objects not the age parameter of the object. So this function:

function olderAge() {
  if (alice > billy) {
    return alice.age;
  } else {
    return billy.age;
  }
}

Should be:

function olderAge(a, b) {
  if (a.age > a.age) {
    return a.age;
  } else {
    return b.age;
  }
}

Putting it all together

I suggest changing your code to this -- we fix the olderAge:

// 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
function olderAge(a, b) {
    if (a.age > b.age){
        return a.age;
    } else {
        return b.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));

JSFiddle: http://jsfiddle.net/5ae24v92/2/

Sign up to request clarification or add additional context in comments.

10 Comments

thank you, this is returning the correct age now. However, i am now getting this error.. Oops, try again. It looks like your olderAge function fails when the first Person is older! It should return the age of the older person
@CASEYBAILEY: I guess the issue is that you are using global variables in olderAge, not the arguments passed to it.
@FelixKling I glazed over that -- I updated with the fix.
thank you for your effor i am now getting "The older person is [object Object]" returned i think it is something to do with codecademy. some of there lessons are stupid specific
@CASEYBAILEY On the JSFiddle, I added a toString method to the Person class. If you add that, the Person object will be converted to a string nicely.
|
1

Try this:

<script>
    // 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
    function olderAge(){
        if(alice.age > billy.age){
            return alice.age;
        }else{
            return billy.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));
</script>

You missed the ".age" in comparison..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.