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 The Build an Object Challenge, Part 2 Solution

Objects not printing to the page?

My code is exactly the same as the video (besides the property values which doesn't matter anyway) but I can't get the objects to print to the page. The JS console doesn't show any errors.

Please help :)

students.js

var students = [
  {
    name: 'Ron', 
    track: 'quidditch', 
    achievments: 1, 
    points: 40
  },
  {
    name: 'Harry', 
    track: 'potions', 
    achievments: 1, 
    points: 60
  },
  {
    name: 'Hermione', 
    track: 'history', 
    achievments: 1, 
    points: 50
  },
  {
    name: 'Luna', 
    track: 'divinations', 
    achievments: 1, 
    points: 20
  },
  {
    name: 'Draco', 
    track: 'darkarts', 
    achievments: 2, 
    points: 20
  }
];

student_report.js

var message = '';
var student;

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

for (i = 0; i < students.length; i += 1) {
  student = students[i];
  message += '<h2>Name: ' + student.name + '</h2>';
}
print(message);

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Students</title>
  <link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>Students</h1>
<div id='output'>

</div>
<script src="js/students.js"></script>
<script src="js/student_report.js"></script>
</body>
</html>

1 Answer

Hey, Leyton. I went ahead and tried out your code and it seems to work as intended in my workspace. Does your workspace look like this one https://teamtreehouse.com/workspaces/10016432 ?

I can't view the link, I think it's linked to your account only.

Sorry about that, didn't know that. You should be able to view this one https://w.trhou.se/ha39z0w6be

Hey, so I went to the next video in the course and previewed the page using all of the code provided by the new workspace and still the site was blank. I think I'm having problems on my end ...

Hey Cory,

So I think I have something weird with my workspaces where when I declared i as a variable it worked

for (var i = 0; i ...

I didn't catch that. It worked because it just created a global variable named i that it worked with. You shouldn't do that, and should declare it with "var".

well, somehow that fixed the problem, and it printed to the page.