Introduction to Objects I 29/33


#1

I can't understand what is wrong with this code, can somebody please help me out? Thanks!!

// 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 your code with proper formatting:

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

Do you see a problem now? You have to close your olderAge function.


#3

I believe I did close my olderAge function, no?

I tried copy and pasting your code and it came up with the same error.

SyntaxError: Unexpected end of input


#4

Well, as I said before - I posted your code with proper formatting.

Take a look at code above and note that these lines:

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

are in the scope of the olderAge function. You can perceive this visually by looking at the indentation levels. These lines have the same indentation level as the rest of the body of olderAge function.

Take a look at my comments to your function:

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

// this is where you should close your function with };

  // lines below are still in the bdy of the function

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

  // this is end of the file, your function is still opened and that is why unexpected end of input has occurred

This is corrected olderAge function:

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

#5

ahhhhhh! you are totes right. Thank you for pointing this out to my stubborn eyes!


#6

You're very welcome :slightly_smiling: Good luck with the next exercises!