Exercise 29/33 quick question


#1

Hey guys! I was able to get the code to pass but there's one thing I would like to know, I did it without putting a way for the age to return equal if the same. How would I accomplish that? I got errors when I tried to add an else if in btw. Thanks a lot guys here's my passed code below.

// 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.age) {
return person1.age;}
else{
return 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));


#2

This is an auxiliary question since the exercise does not expect us to test for equality, only greater age. If we wish to have the added functionality then it would go into the logic of one of the functions.

var ageDifference = function (person1, person2) {
    return Math.round(person1.age - person2.age);
};

The rounded value is returned so 0 is a certain possibility.

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

Of course there are many ways to go about this. You can use the lesson space as a sandbox to experiment in. If there is a lesson at the beginning or end of the track without an SCT, that would be where to test so you get the results you expect with no interference from the lesson checker.


#3

Thanks a lot buddy !