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

Chuck C
Chuck C
3,957 Points

I think your code validation is wrong again. It works in the console but not in your code emulator

var objects = [];

var obj1 = { name: 'Ok', age: 19 } var obj2 = { name: 'Ok', age: 19 } var obj3 = { name: 'Ok', age: 19 } objects.push(obj1, obj2, obj3);

script.js
var objects = [];

var obj1 = {
  name: 'Ok',
  age: 19
}
var obj2 = {
  name: 'Ok',
  age: 19
}
var obj3 = {
  name: 'Ok',
  age: 19
}
objects.push(obj1, obj2, obj3);
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Steven Parker
Steven Parker
231,269 Points

Was the validation wrong before?

Anyway, this time the instructions say "Inside the array literal, add three object literals.". The code shown here does add three objects, but not as object literals, and not done inside the array literal. It's first creating three variables and then dynamically pushing them onto the array.

When following the instructions, you will create only only variable (objects) and have only one statement.

When you try challenges outside the validator, always remember that the IDE has no idea what the actual objective is in the instructions.

Chuck C
Chuck C
3,957 Points

Sorry but I don't get it.

Is this not considered an object literal ? var obj1 = { name: 'Ok', age: 19 }

You say "not as variable". Ok but that's not mentioned ?

So I should do this ? var objects = [{ name: 'Ok', age: 19 }, { name: 'Ok', age: 19 }, { name: 'Ok', age: 19 }];

Steven Parker
Steven Parker
231,269 Points

You got it exactly, 3 object literals inside an array literal. Good job! :+1:

In your example above, "{ name: 'Ok', age: 19 }" is an object literal, but it's not inside the array literal. And "obj1" is a variable that gets added later by a "push" statement.