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

Raymond Rowe
Raymond Rowe
2,331 Points

Can not figure out

I am trying to add three objects with two values each to this array but am unable to do so. I believe the error is in the first few steps of my code as the color indicator is different on objects 2 and 3 then they are on object 1.

script.js
var objects = [
  var obj1 = {
    name : 'Raymond',
    age : 20
  },
  var obj2 = {
    name : 'Shauna',
    age : 20
  },
  var obj3 = {
    name : 'Steve',
    age : 52
  }
];
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

2 Answers

Jamie Reardon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Reardon
Treehouse Project Reviewer

You need to remove the object literal definition expressions from the array. Try this:

var objects = [ 
  { 
    name: 'Raymond',
    age: 20 
  },
  { 
    name: 'Shauna',
    age: 20
  },
  {
   name: 'Steve', 
   age: 52
  } 
];
Raymond Rowe
Raymond Rowe
2,331 Points

Hi Jamie,

I would still need the objects to be defined in that case, where they are not in this example I believe. However, after a little more messing with it I was able to get it working. I took the preceding 'var ' out of each of them and passed the code challenge.

Thank you for responding so quick! -Raymond

Jamie Reardon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Reardon
Treehouse Project Reviewer

You are defining the objects as they are items of the array. You don't need to define them using a keyword like var to create an object literal.