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 trialCarel Du Plessis
Courses Plus Student 16,356 Pointsneed help to understand why one block of code doesn't work and the other one works.
challenge (question)
Inside the array literal, add three object literals. In other words, the objects array should have three values. Each object should have 2 property/value pairs.
why does this code not work?
var objects = [ m:{ m: 1, t: 5 } l:{ m: 1, y: 6 }, r: { t: 5, y: 6 } ];
and this code does work? (what is the difference and what is going on)
var objects = [ m = { m: 1, t: 5 } l = { m: 1, y: 6 }, r = { t: 5, y: 6 } ];
var objects = [
m = {
m: 1,
t: 5
}
l = {
m: 1,
y: 6
},
r = {
t: 5,
y: 6
}
];
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
2 Answers
Steven Parker
231,236 PointsThe syntax of separating a key from a value using a colon is only used in an object, which is identified by enclosing it in braces {}. But plain brackets [] identify an array, so that syntax would not be used there.
The equal signs establish implicit global variables, which while valid, is not a "best practice". A more conventional way to do the same thing might be like this:
var objects = [ { m: 1, t: 5 }, { m: 1, y: 6 }, { t: 5, y: 6 } ];
var m = objects[0];
var l = objects[1];
var r = objects[2];
Also, "l" (lower-case "L") is generally avoided as a variable name because it can easily be confused with 1 (number one).
Carel Du Plessis
Courses Plus Student 16,356 PointsI tried out your answer in the challenge but i received this message Make sure you declare the objects
array with the var
keyword.
Steven Parker
231,236 PointsI wasn't suggesting that as a challenge solution, but as a functional replacement for your sample code.
For the challenge, you only need to define "objects". The other 3 variables are not part of the challenge and just confuse the checker.