0

I have asked a question about creating a method in object literal notation half hour ago. I got good answers, but something is still wrong with my code. I was told to create a new question and here it is. Please don't judge the code by the efficiency. I know that I have used many bio method three times in each object, when I could do 1 function, but I did it to understand more about objects,functions and methods.

So here is my code.

var object1 = new Object()
object1.name = "Neymar";
object1.age = 22;
object1.club = "Barca";
object1.bio = function (){
    console.log(this.name +" is "+ this.age + " years old and he is playing in "+ this.club);
};
var object2 = {
name: "Fred",
age: 28,
club: "Fluminense"
bio2: function (){
    console.log(this.name +" is "+ this.age + " years old and he is playing in "+ this.club);
    };
};
var object3 = {
name: "Hulk",
age: 27,
club: "Zenit St. Petersburg"
bio3: function (){
    console.log(this.name +" is "+ this.age + " years old and he is playing in "+ this.club);
    };
};
object1.bio();
object2.bio2();
object3.bio3();

The CodeAcademy say's that } is missing on the 12th line: bio2: function (){

Community
  • 1
  • 1
GreedyAi
  • 2,709
  • 4
  • 29
  • 55

4 Answers4

2

You forgot to put a comma after the previous property, so it considered it to be the end of the literal. Maybe expected ',' or '}' would've been a better message. Also, inside the object literal you're not allowed to put a line delimiter (semicolon). It should be

var object2 = {
    name: "Fred",
    age: 28,
    club: "Fluminense",
//                    ^
    bio2: function() {
        console.log(this.name +" is "+ this.age + " years old and he is playing in "+ this.club);
    }
//   ^
};
var object3 = {
    name: "Hulk",
    age: 27,
    club: "Zenit St. Petersburg",
//                              ^
    bio3: function() {
        console.log(this.name +" is "+ this.age + " years old and he is playing in "+ this.club);
    }
//   ^
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

You forgot a , after club: "Fluminense" and club: "Zenit St. Petersburg"

Saturnix
  • 10,130
  • 17
  • 64
  • 120
2

your object should look like this,

var object2 = {
   name: "Fred",
   age: 28,
   club: "Fluminense",
   bio2: function (){
      console.log(this.name +" is "+ this.age + " years old and he is playing in "+ this.club);
   }
};

You don't ever have semi-colons in js object's - where you had it anyway, and you shouldn't have a comma on the last item in the object either - it breaks in IE

you also forgot the comma on the line before the function

Quick tip. when debugging javascript the errors could be caused from the line above i recommend going to http://jsfiddle.net and using the jsHint tool that's built in

iConnor
  • 19,997
  • 14
  • 62
  • 97
1

You have missed a common (,) after club: "Fluminense" and club: "Zenit St. Petersburg". You must also remove the end of line delimiter (;) from the ends of your object parameters.

var object2 = {
    name: "Fred",
    age: 28,
    club: "Fluminense",
    bio2: function (){
    console.log(this.name +" is "+ this.age + " years old and he is playing in "+ this.club);
}

};
var object3 = {
    name: "Hulk",
    age: 27,
    club: "Zenit St. Petersburg",
    bio3: function (){
    console.log(this.name +" is "+ this.age + " years old and he is playing in "+ this.club);
}
JosephGarrone
  • 4,081
  • 3
  • 38
  • 61