29/33 syntax error


#1

Alright, folks. I've got a syntax error, but i'll be buggered if I can find it. Anybody have insight into my troubles? Any help is greatly appreciated!

// 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.name;
};
else {
return Person2.name;
};
};

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

Sir,

I do not believe that functions have a semi-color after their closing bracket " }; " I believe you would simply close the bracket for the function " } " and be done with it :smiley:


#3

Try this

// 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.name;
                    }
                    else {return Person2.name;}
                };

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

The problem was on this line

return Person1.name;
};

The semicolon after the closing brace was causing the error


#4

Dude. You're the best! Thank you!

Out of curiosity, is there a rule of thumb for when to//not to use a ";" to end a section of code? Or is it something that just has to be memorized by rote?


#5

And, for the records, here is the updated code that worked. (Also, Person.name was changed to Person.age in the olderAge function to comply with the lesson's requirement)

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


#6

Practice. Objects and normal code are usually ended with a bracket, and I'll try to get you a reference if you'd like.


#7

If you have one easily accessable, that'd be kickass. If not, don't sweat it.


#8

"stackoverflow.com" is probably your best bet for finding most of these conclusive answers.


#9

This will work for you :wink:

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

}