Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Create an Array of Objects

arrays and objects

Why isn't what I have working?

script.js
var objects= [{
 var objects = [ {question: "one + two", answer: "three" }
              {question: "two + two", answer: "four"}
              {question: "three + two", answer: "five"}
              ];





             ];
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

4 Answers

Hi Matt,

It looks like you have two sets of var objects when you only want one. You have the syntax almost correct inside of the array, except that each object has to be separated by a comma except for the very last object.

 var objects = [ 
               //comma inserted after this object
              {question: "one + two", answer: "three" },
               //comma inserted after this object
              {question: "two + two", answer: "four"},
               //no comma here because it is last object
              {question: "three + two", answer: "five"}
              ];

I don't understand the answer.

You start off with an empty array literal and create the variable objects which is equal to that empty array literal which is []. Next, you put those 3 objects into the array. Each object is surrounded by curly braces {}. Each object has to be separated from the object before it, so you need a comma after the ending of each object.

I think I tried that, and it didn't work.

It does work 100%. You must copy and paste correctly.

Whatever...I will just get help from someone else at another time.

I gave you the correct explanation and correct answer. If you copy and paste the code I gave you (which explains what I did), you'll see that the challenge passes. That is a poor attitude to have to someone who's trying to help you.