28/33 syntax error


#1

Can somebody help me identify the syntax error in this code:
// 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;
}

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

// get the difference in age between alice and billy using our function
var diff = ageDifference(alice, billy);{
return alice.age - billy.age;
}
ageDifference();
thx in advance!


#2

An unneeded semicolon. Correct code:

 var diff = ageDifference(alice, billy ){    
     return alice.age - billy.age;
}

#3

// get the difference in age between alice and billy using our function

var diff = ageDifference(alice, billy);

That is all you need to pass the test. We passed to our fuction 2 object Person variables that have .age property.


#4

Now that is roughly what I tried but I capitalised Alice and Billy as that's how they were written in the lessons provided code

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

Does JS automatically change these to lower case? I noticed that the same happens between the Person function and the objects being created by it:

function(person1, person2)

rather than function(Person1, Person2)


#5

This is what I have and it still says "Unexpected token { "

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

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

// get the difference in age between alice and billy using our function
var diff = ageDifference (alice, billy) {
return alice.age - billy.age;
}
ageDifference();


#6

Hey @lagosxc! Instead of return do console.log();


#7

That unfortunately, didn't work :frowning: Still receive the same error "SyntaxError: Unexpected token { "


#8

I am having the same problem , whatever I try, the error message states that I have an unexpected token { :confused:


#9

i get he same error even though i tried console.log instead return


#10

try doing a ; at the end of the )


#11

it worked!! Thanks so much!!
i used the following code:
// 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;
}

var alice = new Person("alice", 30);
var billy = new Person("billy", 25);

// get the difference in age between alice and billy using our function
var diff = ageDifference(alice, billy); {
console.log(alice.age - billy.age);
}


#12

Your code should look like 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;
}

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

// get the difference in age between alice and billy using our function
var diff = ageDifference(alice, billy )
{
console.log(alice.age - billy.age);
}

I hope this could help you


#13

var diff = function ageDifference(name1, name2){
return name1.age - name2.age;

diff(alice, bob);


#14

var diff = ageDifference(alice, billy);


#15

You need to remove the curly brackets from the code on the last part. and console.log the result.

Like this: // get the difference in age between alice and billy using our function
var diff = ageDifference (alice, billy)
console.log( alice.age - billy.age);
ageDifference();