29. Try it Out! -- extension



If I want the code to also tell which one of them is older, is this correct? Or how would I do it..?

// 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
        var bigName = person1.name 
    else if (person2.age > person1.age){
        return person2.age
        var bigName = person2.name 
    else {
        return person1.age
        var bigName = "both of them"

// Let's bring back alice and billy to test our new function
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 98);

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


I went ahead and made it as simple as possible..

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

Worked for me as we were only comparing two people. If it was more, this become more..elaborate. :slight_smile:


@dipityg these are the problems with your code

You sort of have to remove this line because bigName is not defined

So that is really the problem



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