Why doesnt this code work when it should?


#1

this should work but it doesnt

// 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(obj1, obj2){
var al = alice.age
var bi = billy.age
if(al<bi){return bi}else{return al}
}

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

it prints out the older person is 30 and i tested by making billy older aswell, but the lesson wont complete itself
//update//

//i followed one of the guys advice to make this loop
var olderAge = function(obj1, obj2){
var b = ageDifference(alice, billy)
if(b > 0){return alice.age}else{return billy.age}
}
//yet it says the same error, even though it works both ways, so tl;dr javascript says its right but //codecademy says its wrong?

#2

Maybe you need to use ageDifference() inside of olderAge()?


#3

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