29/33 Unexpected token else?


#1

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

Look closely:

See that semi colon after ONLY your if statement? Right here:

The semi colon ends the if statement, getting the else statement stranded and not connecting with the if statement.

Think of it as a wire, one side as the "if" and the other as the "else". That semi colon you put there is like scissors that just cut between it, disconnecting it.

Hope it helped!:smiley:


#3

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

as @ragezapper mentioned.

It's best if you indent your code so that the closing bracket either lines up with your opening bracket, or with the beginning of the statement that started it. Such as below

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

Indenting your code is one of the prime ways to keep your code looking nice, and noticing errors before they start!

I would try to make it a common practice in further lessons :smiley:


--EDIT--

This isn't the correct code. I just took your code and indented it so the errors would be easier to see.