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
200,695 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
200,695 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.