Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?

var objects= [{
 var objects = [ {question: "one + two", answer: "three" }
              {question: "two + two", answer: "four"}
              {question: "three + two", answer: "five"}

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

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.