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

How is the program file able to access the data in the students file?

In the video, Dave creates a separate program file from the data file (students). How is the program file able to access the students file data? I saw that he adds the link in the html.index page but is that really it?

1 Answer

Hello Birdie Bry,

You are indeed correct. It's these two lines that allow the JS code to work together:

<script src="js/students.js"></script>
<script src="js/student_report.js"></script>

You'll notice that in students.js an array of objects is assigned to a variable named students (plural).

This is then used within the for loop inside student_report.js:

for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if ( student.name === search ) {
      message = getStudentReport( student );
      print(message);
    }
}

Using multiple script tags your HTML file allows you to not only include other libraries (such as jQuery), but it also allows you to break up your code.

Hope that helps!

-Brandon