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

Variable students is not defined

It keeps telling me when I copy Dave's code that the variable students is not defined.

How do I allow that variable, which is in a separate JS file, to be defined within my new file without copying it over?

3 Answers

gary franklin
gary franklin
12,309 Points

Also...Make sure that your student property 'name' in students.js is NOT 'Name'.

I'm a bit unclear about from where you're copying the code (following along versus downloading it), but I'll take a whack at the issue.

This might be something as simple as what order you're importing the JS files in index.html. If students.js is imported first, then that variable object is added to the global namespace. You should be able to call it anywhere in JS files after that.

Another issue might be a missed directory or some other typo. if it's not able to find the students.js file, it can't load that variable. Those are the two most common things I've run into with JS.

the code should look like:

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

You may want to make sure that your variable in the student_report.js file is "student" and the array with the student objects you are referencing is "students". I forgot the s for one, even when I followed along with the solution. When I looked at it one more time, I found my error.

So in the javascript:

// at the top your student variable should be singular "student" not "students" 
// remember that this is a completely different variable from the "students" in the other file
var student; // up top

// and in the for loop:

for (var i = 0; i < students.length; i++) {
  student = students[i];
  // rest of the stuff that builds the message, etc.
}
// this takes the data from the student.js (the file with the student information)
// and uses the for loop parameters to cycle through it

The other thing is to double check your javascript file spellings and if they are nested in the js folder or not. Funny how one typo can mess you up.

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