Intro to Objects try it out


#1

I passed the task, but how do I get this code to print the age of who is older?


// 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
  
        
// Let's bring back alice and billy to test our new function
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

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


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


};


#2

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

As the ageDifference result can either be <0 , 0 or >0
you would probably need an IF-ELSEIF-ELSE construct in your olderAge function


#3

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