1

I need to create a JSON object "bio" which contains a lot of variables in it:

var bio = {
"name": "my name",
"role": "Web Developer",
"contacts":
    {
        "mobile": "my phone",
        "email": "my@email.address",
        "github": "https://github.com/",
        "twitter": "https://twitter.com/",
        "location": "Los Angeles, CA"
    },
"welcomeMessage": "Welcome to my online resume.",
"skills": ["HTML5","CSS3","JavaScript","Bootstrap", "Angular", "CoffeeScript", "W3"],
"biopic": "http://placehold.it/150x150",
"display": displayFunc(){

}

};

When I try to run this with:

var formattedName = HTMLheaderName.replace('%data%', bio.name);
var formattedRole = HTMLheaderRole.replace('%data%', bio.role);

$('#header').prepend(formattedRole);
$('#header').prepend(formattedName);

Nothing happens. I believe the error is somewhere in the "contacts" variable because if I comment out contacts and everything below it the name and role show up. But if I comment out welcomeMessage and everything below that I still get nothing.

EDIT: For this class the contact variable is required to be:

contacts : an object with
      mobile: string
      email: string 
      github: string
      twitter: string (optional)
      location: string
Steve Medley
  • 213
  • 1
  • 3
  • 16
  • 2
    That is *not* JSON. Errors in the (JavaScript) syntax it will show up as a Syntax Error in the Console Error Log. Not sure how `bio.contacts` is even "used" given the context.. – user2864740 Jun 30 '16 at 07:37
  • `contacts` is an **array** of **objects**. Not an object itself. – Ivanka Todorova Jun 30 '16 at 07:38
  • Notice that your `displayFunc(){}` needs to be `function displayFunc(){}` – Bubble Hacker Jun 30 '16 at 07:40
  • That isn't JSON, JSON is a textural data format , see: [What is the difference between JSON and Object Literal Notation?](http://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Spencer Wieczorek Jun 30 '16 at 07:41
  • @IvankaTodorova That was a mistake, I tried that after my first attempt didn't work, just pasted the wrong code here. I have "fixed" it now - in that my original non working code is in the post. – Steve Medley Jun 30 '16 at 07:43

1 Answers1

3

If you add function before displayFunc(){} then it should work (as @BubbleHacker mentioned)

Demo here: https://jsfiddle.net/mrLh3pz7/

var bio = {
"name": "my name",
"role": "Web Developer",
"contacts": [
    {
        "mobile": "my phone",
        "email": "my@email.address",
        "github": "https://github.com/",
        "twitter": "https://twitter.com/",
        "location": "Los Angeles, CA"
    }
],
"welcomeMessage": "Welcome to my online resume.",
"skills": ["HTML5","CSS3","JavaScript","Bootstrap", "Angular", "CoffeeScript", "W3"],
"biopic": "http://placehold.it/150x150",
"display": function displayFunc(){}
};
Niklas
  • 13,005
  • 23
  • 79
  • 119