29. Tryi It Out!


#1

Hi guys, I keep getting Syntax Error on the following:

// 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
function olderAge(person1, person2){
    if(person1.age > person2.age){
        return person1.age;
    };
    else if(person2.age>person1.age){
        return person2.age;
    };
    else{
        return person1.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));

Is there something glaringly obvious that I'm missing or is the compiler bugging out?


#2

Remove the semi colons

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

When you put the semicolons there, the any else statement (either normal "else" or "else if" of the if-else) becomes its own statement, and it doesn't go with the syntax.


#3

Oh man.....
The best thing is I'm not even sure why I put the semi-colons in there in the first place!

Thanks Akimbo :slight_smile:


#4

the same problem :smile:


#6

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