Age difference


#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;
this.name = name;
this.age = age;
};

var olderAge = function(person1, person2) {

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

var alice = new Person("Alice", 15);
var billy = new Person("Billy", 20);

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

error SyntaxError: expected expression, got keyword 'else'


#2

@caroldavies

Remember a if-else statement syntax looks like this:

if (condition) {
//Codes run between the curly brackets
}//No need of semicolons
else {
//Codes run between the curly brackets
}//No need of semicolons

#3

Thanks but still not working


#4

@caroldavies
Please print your code again after the changes.


#5

Let's display your code in a more readable fashion.

1  // Our person constructor
2  function Person (name, age) {    
3 	this.name = name;    
4 	this.age = age;
5  }
6 
7  // We can make a function which takes persons as arguments// This one computes the difference in ages between two peoplevar ageDifference = function(person1, person2) {    return person1.age - person2.age;    this.name = name;    this.age = age;     };
8 
9  var olderAge = function(person1, person2) {
10    if (person1.age > person2.age) {
11        return person1.age;
12    }
13    }; 
14    else {
15        return person2.age;
16    };
17
18 var alice = new Person("Alice", 15); 
19 var billy = new Person("Billy", 20);
20
21 console.log("The older person is " + olderAge(alice, billy));};

As what @miniapple8888 mentioned, there's no need for semicolon for if else statements.

Secondly, the curly braces in your if else statement are wrong. Hint: They don't enclose the statements properly.

Lastly, there's an extra curly brace and semicolon at the last line.

Hopefully these helps!